c# - Entity Framework ——CreateQuery VS ExecuteFunction VS ExecuteStoreQuery VS ExecuteStoreCommand

标签 c# entity-framework difference executestorequery

以下有什么区别-

CreateQuery()  ExecuteFunction(), ExecuteStoreQuery() and ExecuteStoreCommand() 

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

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

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.

根据以上信息-

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.

如有错误请指正。任何进一步的信息将不胜感激。

最佳答案

CreateQuery 非常相似的是:

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。前任。您已手动创建映射。

var queryString = "SELECT ... FROM " + tableName;
var table = context.ExecuteStoreQuery<ResultTableTemplate>(queryString );

为您的应用程序编写代码很好,但有时数据库的生命周期比几个用户界面还长。为了最大限度地减少需要完成的工作量,您可以在数据库中存储一些(如果不是全部)功能。它们可以主要作为函数过程存储在那里。

enter image description here

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

var date = ctx.ExecuteFunction<DateTime>("Today").First();

Entity Framework 支持三种加载相关数据的方式——预加载、延迟加载和显式加载。默认情况下它是延迟加载,这就是为什么你看到我使用 .First()/.FirstOrDefault/.ToList/... 有关如何根据需要加载数据的更多信息,你可以采取看Loading Related Entities .

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

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);

奖励:

您想要做的一件常见事情是 call a stored procedure that accepts a table value parameter .

关于c# - Entity Framework ——CreateQuery VS ExecuteFunction VS ExecuteStoreQuery VS ExecuteStoreCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22368794/

相关文章:

c# - 为什么此 AsParallel 操作会抛出数据提供程序错误?

c# - Entity Framework 代码首次迁移 : get sql scripts programmatically

python - 获取具有其他两个绝对差异的数据框

visual-studio-2010 - Ctrl K + D 和 Ctrl E + D 的区别

c# - NServiceBus 中的 SendOnly

c# - LINQ to Entities 无法识别该方法

c# - OnItemUpdated 事件究竟何时在 ASP.NET 的 FormView 中触发?

png - APNG和MNG有什么区别?

c# - ReactiveCommand 的前后 Action

c# - 比较项目列表然后拆分