I'm just getting to grips fully with EF. I'm working on an existing project that isn't using a database diagram or .edmx file, it just defines a model then hooks up the database as a context. I've added some tables to the DB manually and then written the model for the additional tables. Is this a valid approach? It's been giving me lots of problems. What is a good way to approach the problem I'm trying to solve that results in the fewest issues?
-
3Yes, it's a valid approach and necessary when you don't exclusivley own the db schema. Post one of your "problems" as a question.Henk Holterman– Henk Holterman2014-07-13 09:20:06 +00:00Commented Jul 13, 2014 at 9:20
-
Take a look at EntityFramework Reverse POCO GeneratorGert Arnold– Gert Arnold2014-07-13 22:01:22 +00:00Commented Jul 13, 2014 at 22:01
1 Answer
I would suggest instead using migration system. Create first initial migration for the database schema that corresponds to the state of project before your changes. Then apply your changes to the model, generate next migration that will drive the database to stage after your changes and change code of the migration so that to ensure no data loss will occur. To enable migration to work you will need to have __MigrationHistory table in your db. To create it and apply the first migration (which should either way not change anything as the database model should fully correspond to model in your code) you may remove all the code from initial migration and then apply it. With this approach you will need to change only classes, db schema will be changed by migrations.