0

In a project EF 6.1 was used with an existing database using 'Code First From Database' approach as descibed in this article http://msdn.microsoft.com/en-us/data/jj200620

Initially EF generated model classes, later some other classes were added to database and now I need to generate model classes for these new tables as well.

Please guide how I can do that without regenerating everything. Also instead of generating model classes for all tables, is there a way I can select tables to generate models for using same approach.

user576510
  • 5,777
  • 20
  • 81
  • 144
  • I never found a way. I found it easier to just create a new model with the new tables I wanted, then copy the classes and `DBSet` declarations to the old model – cost Sep 24 '14 at 19:32
  • Possible duplicate of [How to synch model after using Code First from Database using Entity Framework 6.1 and MVC 5?](http://stackoverflow.com/questions/22637350/how-to-synch-model-after-using-code-first-from-database-using-entity-framework-6) – Michael Freidgeim Aug 10 '16 at 02:14

2 Answers2

0

Go through the generation process, and during the generation process, it allows you to select tables. Select the newer ones only.

Instead of selecting all tables, expand the little triangle and select what you need. You also will have to select any dependent tables, or else the process will fail. If those dependent tables are already there, just delete the new code (unfortunately for them, there is no way to avoid generation).

enter image description here

There are some more tricks outlined in this here: Reverse engineering a subset of tables for Entity Framework

Community
  • 1
  • 1
Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • 1
    How do you reuse the DbContext? When I try this method, it complains that the DbContext already exists so I have to make a new name (DBContext2), create it, and rename DbContext2 to DBContext. Or delete the DbContext first. – Jack Apr 10 '16 at 11:24
  • I have the same question / issue. – Tsukasa Jan 18 '17 at 23:48
0

One more option is just delete the auto generated classes from the project and once again generate them. While following this approach only thing we need to make sure that is, we should give the same name for the data model(class name which inherits from DbContext ) as the previous one.Data model name is highlighted in below snap. Data Model Name

Rajesh Pawde
  • 399
  • 2
  • 11