Entity Framework not saving changes

60,764

Solution 1

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

Solution 2

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

Solution 3

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
}
Share:
60,764

Related videos on Youtube

Icemanind
Author by

Icemanind

I am a .NET developer. I am proficient in C# and I use ASP.Net Core, Winforms and WPF. I also dabble in React and Xamarin.

Updated on February 24, 2021

Comments

  • Icemanind
    Icemanind about 3 years

    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?

    • Ricky Gummadi
      Ricky Gummadi about 12 years
      assuming you are running against SQL Server :-) have you turned on SQL profiler and see if EF is sending any query to the DB? there should just before any savechanges are done be a login entry in profiler from EF.
    • veblock
      veblock about 12 years
      Have you checked if you hitting context.Products.Add(product); If not, nothing is being saved.
    • Icemanind
      Icemanind about 12 years
      @veblock - I don't have the view for adding products done yet, so I can't test that part yet
  • Chad
    Chad almost 12 years
    Is there a reason that EF doesnt know this with out being told... after all I called the add method. It works on the edit existing
  • veblock
    veblock almost 12 years
    When you edit existing one, you get it from EF first, so EF tracks the state of the object. When you're adding a new one, you create the object outside of EF, hence when you're adding it, you have to explicitly tell EF to add it.
  • Zia Ul Rehman Mughal
    Zia Ul Rehman Mughal over 8 years
    I have looked everywhere and didn't find what i am looking for, i am doing exaclty the same thing when trying to change password. But my changes are not accessible before i restart my solution. It is EF6 and MVC5 asp.net Please help.
  • aruno
    aruno over 7 years
    the AutoDetectChangesEnabled property can greatly improve performance (and memory footprint) for read only queries - so if you find this in your code base don't remove it without checking. if you're just doing a simple update inside a method that is doing a complex read then you may want to create a new data context for the update
  • Roland Pihlakas
    Roland Pihlakas over 4 years
    It is not exactly designed for this purpose, but it may work for your purpose... See stackoverflow.com/questions/31272455/…