What is the best way to add tables to Entity Framework 6 Code First to Existing Database?

asp.net-mvc-5 entity-framework-6


I'm using EF 6 Code First to create an ASP.NET MVC 4.5 project for an existing database. To one of the tables in the dbcontext I've built, I want to build a few new tables with foreign key associations. In that original table, I changed and added columns, resulting in a number of migrations. That table contains actual data.

I don't understand how EF would develop a model for me; instead, I would prefer to create the additional database tables myself. I can write the model's code myself, but I can't find any instructions on how to include it in the context class that EF creates. The migrations would then be chaotic.

Therefore, I believe that dropping the migrations table, deleting the context class, and all migrations would be the best course of action. I may then begin fresh with a first migration. Am I overlooking a gotcha? Exists a better approach?

10/9/2014 2:04:34 PM

Accepted Answer

Just so others having this problem know, I found a solution. Following this answer's 100+ upvotes, I first got rid of all the migrations: Reset migrations in the entity framework

Second, I rebuilt the database's necessary tables and constraints.

Third, I added a brand-new entity to my solution and drew model classes from the database. I switched calls from the former entity to the latter. The model for the original table was overwritten by the generator, but since I have all the annotations in version control, pasting them in is simple.

I can re-enable migrations if I need to.

Hope this was useful.

5/23/2017 12:05:50 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow