3

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?

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • I don't think Database First ever *removes* anything from the Conceptual Scheme when "Updating from Database" (i.e. only removes from the Storage Scheme). But I can't find a citation. I've only ever seen people removing things from the Conceptual Scheme manually, which does feel weird. [This answer sort-of explains how it works.](http://stackoverflow.com/a/2188799/7724) But in your case, it's counter-intuitive that EF would see the removed column as a customization in your Conceptual Scheme since it was mapped between the Storage Scheme and the Conceptual Scheme automatically. – bzlm Mar 25 '14 at 08:26

1 Answers1

3

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

Hoghweed
  • 1,938
  • 1
  • 16
  • 35
  • 1
    If Update Model From Database is failing, On the diagram select all then delete and do Update Model From Database.. Worked for me. – PAVITRA Oct 31 '15 at 17:50
  • 1
    @PAVITRA, your comment should be the accepted answer, though I was able to delete just the specific tables relevant to my task, and then re-add then during the "Update Model from Database" wizard. – David Aug 09 '16 at 20:56