C# 实体框架不保存更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9559104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework not saving changes
提问by Icemanind
I've got an MVC web application that uses SQL Server 2008 as a back end database along with the Entity Framework. The application is working fine and pulling data from the database just fine. My problem is, when it does an update to the data, it doesn't appear to be saving it. I am using the follow function:
我有一个 MVC Web 应用程序,它使用 SQL Server 2008 作为后端数据库以及实体框架。该应用程序运行良好,从数据库中提取数据也很好。我的问题是,当它更新数据时,它似乎没有保存它。我正在使用以下功能:
public void SaveProduct(Product product)
{
if (product.ProductID == 0)
{
context.Products.Add(product);
}
context.SaveChanges(); // Breakpoint here
}
This function is defined in my repository code. I set a breakpoint on the line commented above and the application is breaking at the line, so I know its hitting that line and the changes are all good in the context object. No error occurs, EF simply isn't saving the changes for some reason.
这个函数是在我的存储库代码中定义的。我在上面注释的行上设置了一个断点,应用程序在该行中断,所以我知道它击中了该行,并且上下文对象中的更改都很好。没有发生错误,EF 只是出于某种原因没有保存更改。
I believe my connection string is correct, since its pulling the data just fine, but here it is just in case:
我相信我的连接字符串是正确的,因为它可以很好地提取数据,但这里是为了以防万一:
<connectionStrings>
<add name="EFDbContext" connectionString="Data Source=localhost;Initial Catalog=WarehouseStore;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>
</connectionStrings>
Anyone have any ideas on what could cause this?
任何人对可能导致这种情况的原因有任何想法?
采纳答案by veblock
If you are after the insert/update functionality you have to cover both cases:
如果您使用插入/更新功能,则必须涵盖这两种情况:
if (product.ProductID == 0)
{
context.Entry(product).State = EntityState.Added;
}
else
{
context.Entry(product).State = EntityState.Modified;
}
context.SaveChanges();
回答by HamidReza
you can use Create method of context : use this method generally when you have a related entity
您可以使用上下文的创建方法:当您有相关实体时,通常使用此方法
public void SaveProduct(Product product)
{
if (product.ProductID == 0)
{
product = context.Products.Create();
product.property = ...;
product.property = ...;
context.Products.Add(product);
}
context.SaveChanges(); // Breakpoint here
}
回答by Stefano Altieri
Thanks to @veblok I found the solution to my issue. There is an option in the DbContext class to prevent EF to track object by default. Once removed it EF started behaving as expected.
感谢@veblok,我找到了问题的解决方案。默认情况下,DbContext 类中有一个选项可以防止 EF 跟踪对象。删除后,EF 开始按预期运行。
public class My Context : DbContext {
public MyContext()
{
// REMOVE this or use veblok's solution
this.Configuration.AutoDetectChangesEnabled = false;
}
...
}
回答by mirushaki
I had the same problem. context.Entry(product).State = EntityState.Modified;gave me an error. Interesting solution is to use DbSetMigrationsExtensions.AddOrUpdate()Method.
This Method is part of DbSetMigrationExtensions and you need to specify the following namespace in order to use this extension method:
我有同样的问题。context.Entry(product).State = EntityState.Modified;给了我一个错误。有趣的解决方案是使用DbSetMigrationsExtensions.AddOrUpdate()方法。此方法是 DbSetMigrationExtensions 的一部分,您需要指定以下命名空间才能使用此扩展方法:
using System.Data.Entity.Migrations;
public void SaveProduct(Product product)
{
context.Set<Product>().AddOrUpdate(product);
context.SaveChanges();
}
回答by flashsplat
My issue was slightly different. My model said the DB was assigning the key, but I was actually creating it in my controller and passing it. I commented out the line below and everything started working:
我的问题略有不同。我的模型说数据库正在分配密钥,但我实际上是在我的控制器中创建它并传递它。我注释掉了下面的行,一切都开始工作了:
Best of luck to anyone else who may run into this. Was a bugger to find!
祝其他可能遇到这种情况的人好运。是个笨蛋!


