none
context.SaveChanges() not persisting data in database RRS feed

  • Question

  • Hello, Im working on a MVC app.

    When I call context.SaveChanges to update a specific product. The update is not registered in the database. I do not get any runtime error either. All in notice is that my product catalog is not updated. I still see the same values. When u run the debugger i notice that the connection state of the database is closed??

    What seems to be the problem?

    Friday, August 24, 2012 3:26 PM

Answers

  • I put context.entry(product).state = EntityState.Modified; above the context.SaveChanges() method and the database now gets updated. Thanks for all your help

    • Marked as answer by toBbie Saturday, October 20, 2012 8:53 PM
    Saturday, October 20, 2012 8:51 PM

All replies

  • Hi,

    Can you show some code demonstrating how you are changing your entities? The most obvious answer is that none of your entities are in the added or modified state.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Friday, August 24, 2012 3:28 PM
    Moderator
  • This is the class implementing the context.SaveChanges()

    namespace SportsStore.Domain.Concrete

    {
       public class EFProductRepository : IProductRepository
        {
           private EFDbContext context = new EFDbContext();



           public IQueryable<Product> Products
           {
               get { return context.Products; }
           }


           public void SaveProduct(Product product)
           {
               if (product.ProductID == 0)
               {
                   context.Products.Add(product);
               }

    context.SaveChanges();

           }
        }
    }

    Here is my DBContext class

    namespace SportsStore.Domain.Concrete
    {
       public  class EFDbContext : DbContext
        {
            public DbSet<Product> Products { get; set; }
        }
    }

    Here us my Product class

    namespace SportsStore.Domain.Entities
    {
        public   class Product
        {

            [HiddenInput(DisplayValue=false)]
            public int ProductID { get; set; }

            public string Name { get; set; }

            [DataType(DataType.MultilineText)]
            public string Description { get; set; }

            public string Category { get; set; }

            public decimal Price { get; set; }
        }
    }

    I don't get any runtime error I just notice that my database is not updated. i still see the same values.

    I'm reading the book Pro ASP.NET MVC 3 and their code seems to work fine. 

    How do I get to the Stack Overflow community. Thanks Glenn

            
    Friday, August 24, 2012 3:50 PM
  • Hi,

    Stackoverflow is at http://www.stackoverflow.com/.

    The code looks like it probably should work, what database are using? By default EF will connect to a local SQL Express or local data database depending on the versions of Visual Studio and EF. See here http://thedatafarm.com/blog/data-access/vs11-and-ef5-where-rsquo-s-that-database-that-code-first-created/.

    If you are connecting and querying the correct database then that rules out the most common scenarios for this, so I'd get you to send me the solution if you can and I can look at it that way. Let me know if that's the case and I'll give you an address to send it to.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Friday, August 24, 2012 4:18 PM
    Moderator
  • Hi Glenn give me the address so i'll send the solution to u.
    Friday, August 24, 2012 4:51 PM
  • Just send it to glennc 'AT' Microsoft 'dot' com.

    I'll take a look and see if I can tell you why it isn't working.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Friday, August 24, 2012 4:56 PM
    Moderator
  • Hi,

    In some cases this is just because you don't check the appropriate database file (for example SQL Server Express with an attached file). The mdf file is written to the output directory and you check the one that is in the solution or if the mdf file is overwritten with each build you don't see the added file on the next run.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, August 24, 2012 6:31 PM
  • Hey Glenn. I've sent the solution and the database to your email: [removed] The database  resides  in a SqlServer express edition. Thanks
    Friday, August 24, 2012 6:59 PM
  • Hi I don't understand your response. D database i use resides in sqlserver. And it works fine cos the app reads the product catalog from it. Its just that. updates r not reflecting in the database
    Friday, August 24, 2012 7:03 PM
  • Does it work for inserts ?

    I meant that sometimes one reports :
    - no error
    - updates or inserts seems to not be taken into account, actually when this is double checked it works when reading data back in the same run from within the app but seems to disappear later.

    This is usually caused by :
    - using SQL Server Express with an attached file and missing rows are not seen because the MDF file that is part of the solution is checked rather than the one that is copied to the output directory
    - and/or the MDF file property is set to "overwrite" so that the MDF file is always overwritten in each build so changes are not seen anymore on the next run

    My first step would be either to use SQL Server trace tool or to check the return value for context.SaveChanges to find out if something does make it the db. If you do see something, double check you are not in the above case. Else I would then use the ChangeTracker property to see if EF doesn't see the change for some reason.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Friday, August 24, 2012 8:18 PM
  • I put the code segment below: just above the context.Savechanges() method and the application ws able to persist data after edits

     context.Entry(product).state = EntityState.Modified;

    Saturday, October 20, 2012 8:47 PM
  • I put context.entry(product).state = EntityState.Modified; above the context.SaveChanges() method and the database now gets updated. Thanks for all your help

    • Marked as answer by toBbie Saturday, October 20, 2012 8:53 PM
    Saturday, October 20, 2012 8:51 PM
  • i am try to implement this code but it will give error in below line i.e Entry is not available for that i add reference  system.web.mvc, system.data.entity,etc

    context.Entry(product).state = EntityState.Modified;

    please tell me how to solve this issue as soon as possible early.

    Friday, May 10, 2013 11:38 AM