是的SaveChanges()与必要进口功能(存储过程)? [英] Is SaveChanges() Necessary with Function Imports (Stored Procedures)?

查看:148
本文介绍了是的SaveChanges()与必要进口功能(存储过程)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的SaveChanges()必要使用功能的进口(存储过程)



?例如:

 无效美孚(产品产品)
{
// addProduct命令是一个存储过程$ b $的函数,进口b entities.AddProduct(product.Name,product.Price,product.Description);

entities.SaveChanges(); //这个必要吗?
}


解决方案

据的MSDN 的SaveChanges




仍然存在的所有更新数据源并重置改变
对象上下文跟踪。




也就是说,对于被附加到上下文并使用已增加,修改或删除的任何实体,EF将生成相应的SQL代码并运行它针对数据库。在你的情况,你已经通过调用 addProduct命令存储过程(或多或少)直接对数据库运行SQL代码。所以你的情况的SaveChanges 不会做任何事情,是没有必要的(除非你对课程的ObjectContext的其他未保存的更改)。


Is SaveChanges() necessary with function imports (stored procedures)?

Example:

void foo(Product product)
{
    // AddProduct is a function import of a stored procedure
    entities.AddProduct(product.Name, product.Price, product.Description);

    entities.SaveChanges(); // Is this necessary?
}

解决方案

According to MSDN, SaveChanges

Persists all updates to the data source and resets change tracking in the object context.

That is, for any entities that are attached to the context and which you have added, modified or deleted, EF will generate the corresponding SQL code and run it against the database. In your case you are already running SQL code (more or less) directly against the database by calling the AddProduct stored procedure. So in your case SaveChanges won't do anything and is not necessary (unless you have other unsaved changes on the ObjectContext of course).

这篇关于是的SaveChanges()与必要进口功能(存储过程)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆