Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to mix database first and code first models with entity framework?

I am about to begin a web application where I would like to use the Entity Framework with (mostly) code first models.

However, in addition to the application-specific models I plan to create, I have to use an external user database. Is it possible to specify one of my models as database first and to use a separate database context?

like image 976
drewwyatt Avatar asked Jul 10 '14 15:07

drewwyatt


People also ask

Which is better code first or database first in Entity Framework?

Versioning databases is hard, but with code first and code first migrations, it's much more effective. Because your database schema is fully based on your code models, by version controlling your source code you're helping to version your database.

Can we use code first with existing database?

This step-by-step walkthrough provides an introduction to Code First development targeting an existing database. Code First allows you to define your model using C# or VB.Net classes. Optionally additional configuration can be performed using attributes on your classes and properties or by using a fluent API.

What is the difference between code first model first and database first?

In the code first approach, the programmer has to write the classes with the required properties first, while in the database first approach, the programmer has to create first the database using GUI.

How do I convert code first to database first?

There is no way to convert your code-first classes into database-first classes. Creating the model from the database will create a whole new set of classes, regardless of the presence of your code-first classes. However, you might not want to delete your code-first classes right away.


1 Answers

Technically, it's possible, but I wouldn't recommend it. It's far better to just use code-first across the board. Yes, ironically, you can use "code-first" with an existing database.

Just create POCOs that match the tables in your existing database. If your POCO is not named the same as your table (not all table names would be valid or appropriate class names), you can use the Table attribute to explicitly tell EF what table your POCO works with:

[Table("SomeTable")]
public class MyAwesomeEntity
{
    ...
}

Then, you'll need a separate context specifically for this existing database and any entities that belong to it. All you have to do is 1) tell it what connection string it should use and 2) turn off database initialization, so EF doesn't try to actually create the database.

public MyExistingDatabaseContext : DbContext
{
    public MyExistingDatabaseContext()
        : base("MyExistingDatabaseConnectionStringName")
    {
        Database.SetInitializer<MyExistingDatabaseContext>(null);
    }

    // DbSets here
}

And that's it. Whenever you need to work with an entity from this existing database, just new up this context or get it some other way, such as through a DI (dependency injection) container, and go to town.

like image 72
Chris Pratt Avatar answered Oct 03 '22 19:10

Chris Pratt