I have to create some new entities in a new or existing database using Entity Framework that will need to interact with some legacy tables and I'm wondering what the best approach is here. I was hoping to use Code First with migrations.
For example, say I have an existing populated Person table and I need to create a Animal table that will contain a PersonId foreign key to reference the existing people.
As far as I can tell these are my options:
- Create a new
DBContext(for a new DB) with anDBSet<Animal>, using code first migrations and POCO entity classes. Ran into problems with setting up the foreign key pointing to another DB. Create a new
DBContext(targeting the existing DB) withDBSet<Animal>and create some kind of POCO wrappers around the existing table. I'm not sure if this is possible - I tried something like but when applying the migration EF tried to create thePersontable. I assumed this would map to the existing table instead of creating a new one:[Table("Person")] public class Person {Use Database first with the existing DB and create the tables in the existing DB but then I lose out on using POCO and migrations with my new entites.
Are there any better options that I'm missing or should I go ahead with using Database first?
OnModelCreatingevent), along with some selective manual edits of the migration scripts themselves to accommodate what already exists. You can dig as deep as you need to into EF, and people do this regularly to make it work with a wide variety of circumstances.