1

I'm developing an ASP.NET MVC 4.5 project using EF 6 Code First to an Existing Database. I would like to create some new tables with foreign key relationships to one of the tables in the dbcontext I've created. I've altered and added columns in that original table, creating several migrations. There is real data in that table.

I would prefer to create the new tables in the database, but don't see how EF would generate a model for me. I can code the model myself, but don't see any documentation about how I would add it to the context class generated by EF. And then the migrations would be out of whack.

So I'm thinking that the best thing to do would be to delete all the migrations, delete the context class and drop the migrations table. Then I could start from scratch with an initial migration. Am I missing some gotcha? Is there a better way?

JaneH
  • 359
  • 3
  • 15
  • You can generate a code first model from an existing database using the latest EF Tools in VS 2012/2013 – ErikEJ Oct 09 '14 at 15:09
  • Yes, that is exactly what I did to begin with. Now I am asking the best way to proceed in the situation I describe in my question. – JaneH Oct 09 '14 at 16:26
  • I got confused by your use of the Word "table" for classes in your dbContext – ErikEJ Oct 09 '14 at 16:34

1 Answers1

0

FWIW to others facing this dilemma, I figured it out. First I got rid of all the migrations, following the 100+ up-voted answer here: Reset Entity-Framework Migrations

Second, I created new the tables and constraints I needed in the database.

Third, I created a new entity in my solution and generated model classes from the database. I changed calls from the old entity to the new entity.The generator overwrote the model for the original table, but since I have all the annotations in version control, it is trivial to paste them in.

If I need to, I can enable migrations again.

Hope this helps.

Community
  • 1
  • 1
JaneH
  • 359
  • 3
  • 15