How to update only one table for model from databa

2019-03-10 20:07发布

问题:

I have a model generated from db with Entity Framework. When I have any change in database, I update model from database to get the change in model. But this update is applied to all entities (tables) included in model.

Now I add a new column in a table Tab1. I don't want to update model from database as some other changes that I don't want to include in model. I can add the new property in model for entity Tab1 manually. then it caused mapping error.

So I need to update Model.Store for the table to include the new column. It means I want to update model only for Tab1.

How can I do this?

回答1:

The EDMX file is an XML file that is a combination of 3 different parts that make up the whole thing. If you right-click on your EDMX file and choose "Open with... XML Editor" you'll see the 3 different sections:

  • <edmx:ConceptualModels>
  • <edmx:StorageModels>
  • <edmx:Mappings>

These sections can be edited manually, at your own risk! :-)
That way you can modify only what you need to.

Note that it's also possible to generate CSDL, SSDL & MSL files rather than having them embedded in the binary file, by changing the "Meta Artifact Processing" property of your model to "Copy to Output Directory".

If you don't want to do this manually, there's the Huagati DBML/EDMX tool, it is free and you can download it from huagati official site or from visual studio gallery, which is a Visual Studio plugin that allows you to select what changes need to be done.



回答2:

I use following (Conditional) trick. This could be done only when no Table depends on the table which you want to update.

  1. Delete the table which needs to be updated.
  2. Right click on Model and select 'Update Model From Database'. The Table would be shown in the tab 'Add'. Select this table and Update the model.

Precaution : If other existing tables have changes in them, EF would update these changes as well.



回答3:

There is way of doing it automatically.

right click edmx file > update model from data base > Refresh tab > Tables > select the table(you want to update) and press finish that's it.