首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实体框架- CreateQuery VS ExecuteFunction VS ExecuteStoreQuery VS ExecuteStoreCommand

实体框架- CreateQuery VS ExecuteFunction VS ExecuteStoreQuery VS ExecuteStoreCommand
EN

Stack Overflow用户
提问于 2014-03-13 03:59:18
回答 1查看 9.2K关注 0票数 11

以下几点有什么区别-

代码语言:javascript
复制
CreateQuery()  ExecuteFunction(), ExecuteStoreQuery() and ExecuteStoreCommand() 

据我所知,CreateQuery用于实体SQL &其余的方法用于sql函数或在DB中定义的存储过程。

根据ObjectContext类元数据,它们如下所示:

代码语言:javascript
复制
CreateQuery():Creates an System.Data.Objects.ObjectQuery<T> in the current object context        by using the specified query string. 
Returned -> System.Data.Objects.ObjectQuery<T>


ExecuteFunction(): Executes a stored procedure or function that is defined in the data source and expressed in the conceptual model; discards any results returned from
the function; and returns the number of rows affected by the execution.
Returned -> The number of rows affected.
This has an overloaded version which return -> The entity type of the System.Data.Objects.ObjectResult<T> 


ExecuteStoreCommand(): Executes an arbitrary command directly against the data source using the existing connection.
Return -> The number of rows affected.


ExecuteStoreQuery(): Executes a query directly against the data source that returns a sequence of typed results.
Return -> An enumeration of objects of type TResult.

根据上述资料-

代码语言:javascript
复制
Use ExecuteFunction() if you have added db Function/Stored Procedure in your EDMX & can be used for both insert/update & getting result set.

Use ExecuteStoredCommand() if you have not added db Function/Stored Procedure in your EDMX & can be used to insert/update only.

ExecuteStoreQuery() can do what Executefuction() can do except that you no need to add your db Function/Stored Procedure in EDMX & IEnumerable can be used as return type.

如果我错了,请纠正我。如有任何进一步资料,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-12 14:54:56

CreateQuery非常相似的是:

代码语言:javascript
复制
var id = 42;
using(var ctx = new Entities()){
  var query = ctx.Companies.Where(o=>o.Id==id);
  var result = query.First();
}

在调用First()之前,查询只是一个查询。没有任何东西被发送到数据库。只有当数据被询问时,查询才会被执行,数据才会被检索。

编写lambda很容易,但让我们说,您可以为了其他好处而牺牲它。如果EDMX不知道您的数据映射,基本上只能使用ExecuteStoreQuery.。例如。您已经手动创建了映射。

代码语言:javascript
复制
var queryString = "SELECT ... FROM " + tableName;
var table = context.ExecuteStoreQuery<ResultTableTemplate>(queryString );

为您的应用程序编写代码是可以的,但有时数据库比几个用户接口的寿命更长。为了减少需要完成的工作量,您可以在数据库中存储一些功能(如果不是全部的话)。它们可以主要作为函数、过程存储在那里。

ExecuteFunction只适用于函数导入。函数是计算值,不能对Server执行永久的环境更改(即不允许插入或更新语句)。要在Select Today()中调用自定义函数C#:

代码语言:javascript
复制
var date = ctx.ExecuteFunction<DateTime>("Today").First();

实体框架支持三种加载相关数据的方法--急切加载、延迟加载和显式加载。默认情况下,这是延迟加载,这就是为什么您看到我使用.First()/.FirstOrDefault/.ToList/...获取关于如何加载数据的更多信息,因为您需要时可以查看加载相关实体

过程就像数据库批处理脚本。如果您首先使用数据库设计,那么您的大部分业务逻辑很可能将存储在过程中。您可以在c#中调用它们,比如:

代码语言:javascript
复制
var cmdText = "[DoStuff] @Name = @name_param, @Age = @age_param";
var @params = new[]{
   new SqlParameter("name_param", "Josh"),
   new SqlParameter("age_param", 45)
};

ObjectContext.ExecuteStoreQuery<MyObject>(cmdText, @params);

奖金

您想要做的一件常见的事情就是调用接受表值参数的存储过程。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22368794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档