简体   繁体   中英

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. 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.

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. There is an option in the DbContext class to prevent EF to track object by default. Once removed it EF started behaving as expected.

 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. This Method is part of DbSetMigrationExtensions and you need to specify the following namespace in order to use this extension method:

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

entity.HasNoKey();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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