Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sync model after using Code First from Database using Entity Framework 6.1 and MVC 5?

Assumptions

  • Using EF 6.1, MVC 5, VS 2013, C#

  • I have an existing database model designed in Toad DM for SQL Server and it's very important keep it always updated

Steps and Notes

  1. Using ADO.NET Entity Data Model I chose Code First from Database (new feature in EF 6.1) to generate the models. Note: Model classes and DbContext class generated successfuly but NO .edmx or .tt file was generated.

  2. Next I added a new scaffold item: MVC 5 Controllers with views, using Entity Framework. Note: Success, controllers and views generated

Question

From now on I don't want to use Code First to update my database. Instead I want the models to be updated based on database changes. What to do next? If I don't have an edmx file will I not be able to update my model classes from the database?

like image 587
gkaio Avatar asked Mar 25 '14 14:03

gkaio


People also ask

How do I update my Entity Framework database 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.


2 Answers

The Entity Data Model Wizard's Code First from Database does an excellent job creating your entity classes, as if they were created in the Code First style. What you are asking is if there is any way to keep these classes up-to-date as your database changes, similar to the EDMX style "Update Model From Database". From what I've researched this is not possible using the built-in tooling. However, here is one workaround that I've found useful:

Let's say I have database with a product table and customer table. Originally I created a StoreDBContext class, and selected product as one of my objects. Now I want to add the customer table as a new entity to the existing context. Here's how to do this using the Code First Wizard:

  1. Create a new Entity Data Model, call it StoreDBContextTemp or whatever Add new item
  2. Choose the code first from database wizard option code first from database
  3. Select customer as an object to add (just customer) & complete the wizard add table
  4. Open the newly created context file, StoreDBContextTemp.cs, and copy the virtual properties of your newly added entities:

public virtual DbSet<Customer> Customers {get; set;}

  1. Paste these new properties into your Original StoreDBContext.cs dbcontext class.
  2. Delete StoreDBContextTemp.cs, and remove the connection string for StoreDBContextTemp in app.config/web.confg etc.
  3. You can now use Customer on the StoreDBContext class

If you add or remove tables you will need to manually adjust fields, but at least you won't need to hand write dozens of properties each time a new table is added to the model.

like image 74
Brian Vander Plaats Avatar answered Oct 19 '22 03:10

Brian Vander Plaats


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

like image 39
Rajesh Pawde Avatar answered Oct 19 '22 04:10

Rajesh Pawde