Entity Framework does not clear data in database

I am using Visual Studio 2012. I have a very simple product table in a local database (artfully named Database1). It has two fields: id and Name. I preloaded it with some test data.

The following code should update the name of the first product. Inside it is. I can get all the products and see that the first name is "Shirt." But this change is never flushed to the database. The record is never updated. Examining the database shows that the name has not been changed.

My question is simple: why the changes are not sent to the database?

using (var context = new Database1Entities()) { var products = context.Products; products.First().Name = "Shirt"; context.SaveChanges(); } 

Thanks in advance.

= - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =

EDIT:

I tried my best to enter the full code and output it here. But no matter what I do, I keep getting the error message "Your message seems to contain code that is not formatted correctly." After 30 minutes I give up.

EDIT:

Code and output here: http://pastebin.com/GmQtwAND

+4
source share
2 answers

I discovered a problem ... and its solution.

The Copy to Output Directory property of the database file has been set to Always Copy. This, of course, meant that every time the application was created, a new copy was placed in the Bin directory. It was fixed to set it to "Copy if new." Doh.

In other words, the changes were saved in the database, but then the database was crashed when the application was restored.

+3
source

Well, this may mean that you are not tracking changes, how about how you try to set State to → Modified and see if this works:

 var product = products.First(); product.Name = "Shirt"; product.State = EntityState.Modified; context.SaveChanges(); 

If you want to enable it for context, you can do it like this:

 context.Configuration.AutoDetectChangesEnabled = true; 
+2
source

Source: https://habr.com/ru/post/1484557/


All Articles