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:
- Create a new Entity Data Model, call it StoreDBContextTemp or whatever
- Choose the code first from database wizard option
- Select customer as an object to add (just customer) & complete the wizard
- 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;}
- Paste these new properties into your Original
StoreDBContext.cs
dbcontext class. - Delete StoreDBContextTemp.cs, and remove the connection string for StoreDBContextTemp in app.config/web.confg etc.
- 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.