Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update only one table for model from database with Entity Framework?

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?

like image 819
KentZhou Avatar asked Jun 07 '13 13:06

KentZhou


People also ask

How do you update a table in Entity Framework first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

How do I update my EF core model after Database change?

Right-click anywhere on the design surface, and select Update Model from Database... In the Update Wizard, select the Refresh tab and select your table then click Finish button.


2 Answers

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.

like image 55
MaxSC Avatar answered Oct 01 '22 23:10

MaxSC


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.

like image 33
Abhijeet Nagre Avatar answered Oct 01 '22 23:10

Abhijeet Nagre