简体   繁体   English

实体框架不保存更改

[英]Entity Framework not saving changes

I've got an MVC web application that uses SQL Server 2008 as a back end database along with the Entity Framework.我有一个 MVC Web 应用程序,它使用 SQL Server 2008 作为后端数据库以及实体框架。 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:我正在使用以下功能:

    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?任何人对可能导致这种情况的原因有任何想法?

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

Thanks to @veblok I found the solution to my issue.感谢@veblok,我找到了问题的解决方案。 There is an option in the DbContext class to prevent EF to track object by default.默认情况下,DbContext 类中有一个选项可以防止 EF 跟踪对象。 Once removed it EF started behaving as expected.删除后,EF 开始按预期运行。

 public class My Context : DbContext {
  public MyContext()
  {
        // REMOVE this or use veblok's solution
        this.Configuration.AutoDetectChangesEnabled = false;            
  }
  ...
 }

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
}

I had the same problem.我有同样的问题。 context.Entry(product).State = EntityState.Modified; gave me an error.给了我一个错误。 Interesting solution is to use DbSetMigrationsExtensions.AddOrUpdate() Method.有趣的解决方案是使用DbSetMigrationsExtensions.AddOrUpdate()方法。 This Method is part of DbSetMigrationExtensions and you need to specify the following namespace in order to use this extension method:此方法是 DbSetMigrationExtensions 的一部分,您需要指定以下命名空间才能使用此扩展方法:

using System.Data.Entity.Migrations;

public void SaveProduct(Product product)
    {
        context.Set<Product>().AddOrUpdate(product);

        context.SaveChanges();
    }

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!是个笨蛋!

Be sure your table has a primary key.确保你的表有一个主键。

If so, you will find this line in the dbcontext如果是这样,您将在 dbcontext 中找到这一行

entity.HasNoKey();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM