0

I'm using Entity Framework with Code First in C#.

I'm trying to display the contents of one of the tables from my database using the DbSet but it differs from the actual content of that table in the database. I'm looking at the DbSet and the table at the same time but the DbSet hasn't been updated.

This is what I see: The table in the database

The DbSet while debugging

I would like to understand why the element number 4 of the set has a different type than the others.

If I close the program and re open it then the DbSet gets updated.

Thanks

moondaisy
  • 4,303
  • 6
  • 41
  • 70
  • 0-3 are proxy classes, used for lazy loading. Number 4 is some instance you Attach()ed, probably posted through MVC. But that's not your question, is it? Your view lacks an input element for the 'EstaFinalizado' column, so the entity you're trying to save has the default value of bool, being false. This incorrectly updates your database record. In case of an update, you need to load the existing entity from the database first, then assign the appropriate properties from the posted model and save the changes. – CodeCaster Jun 09 '15 at 21:42
  • I don't kwon if that's the case, but keep in mind any changes to the database that happens after the entity have been hydrated, won't reflect on your object since the query will be executed only once. – bateloche Jun 09 '15 at 21:46
  • The type of the number 4 is different because you expanded the three – bateloche Jun 09 '15 at 21:47

1 Answers1

0

The element number 4 is an element you just created so the EF does not have created a proxy for it. The problem that the element does not reflect the database changes could be several. For example,
- the element 4 is just created in memory than someone else (another context) update the element. - the element 4 is just created in memory with some null fields and SQL addedd some default values.
- ...

bubi
  • 6,414
  • 3
  • 28
  • 45