Updating models from database in Entity Framework 6.0

ado.net-entity-data-model c# entity-framework entity-framework-6 sql-server-2008

Question

I'm developing a Web API Service (in Visual Studio 2013), and I've added, in my solution, ADO.NET Entity Data Model (Database First).

All the tables I've included, are correctly created in my edmx diagram.

Then I've made some changes: I've added two new columns in the source database, and I've removed one column. So, when I try to update model from database the two new columns are added, while the removed column is still in my model and the message:

Error 11009: Property 'Image' is not mapped.

Where Image is the name of the removed column in the database.

Can I manually remove this property from model? Or is there another cleaner and safer way to do this?

1
2
2/3/2015 8:32:08 PM

Accepted Answer

Sure, as others says, changing the the approach could be a better way to work with EF and DB, but starting from the fact you need Database First, I think working with Database First Model has it's advantages, one of those (and from this depends the answer to your second question) is the EDMX is taken as is, this implies as a generated piece of code, you can refresh, destroy and recreate that every time you need or want without loosing any modification.

Now, as I said before, the nature of generated code, implies every change you made to the EDMX will be lost, so sure you can remove a property, but the model and db are not transparently in sync.

I think the best way to do that is

  1. Update the database structure
  2. Update the EDMX, if the update "fails", meaning the changes are not reflected, destroy EDMX and then recreate it

Even if this way could be a simplistic approach, doesn’t cheating the nature of database first.

Just a note: working with database first, could be assisted by using partial classes to add things to the DbContext and entities involved to add functionalities and behaviours..

3
3/25/2014 8:34:47 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow