Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code-first default data in database

How do I handle situations in which I need pre-existing data before the app is started or right after the database is generated. For example, I have a list of countries in which I'd like to load into the database after code-first generates it. How do I do this?

App is structured as follows:

Repository > Service > WebMVC

The xml is in the WebMVC project.

like image 613
Shawn Mclean Avatar asked Apr 13 '11 21:04

Shawn Mclean


People also ask

How do I code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

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

3)Database Version Control 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.


2 Answers

You create custom initializer, which inherits from DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways interface. Like:

public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<-YourDbContext-> 

And then you overwrite Seed method like:

protected override void Seed(YourDbContext context) 

Whole example might look like:

public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<EntitiesContext> {     protected override void Seed(EntitiesContext context)     {         List<Role> roles = new List<Role>         {             new Role {Id=1, Title="Admin"},             new Role {Id=2, Title="ProjectManager"},             new Role {Id=3, Title="Developer"}         };          // add data into context and save to db         foreach (Role r in roles)         {             context.Roles.Add(r);         }         context.SaveChanges();      } } 

Edit: After setting this up, you have to set up Initializer too, as Ladislav Mrnka mentioned.

Database.SetInitializer(new EntitiesContextInitializer()); 

ie.: in Global.asax:

protected void Application_Start() {     AreaRegistration.RegisterAllAreas();     RegisterGlobalFilters(GlobalFilters.Filters);     RegisterRoutes(RouteTable.Routes);     Database.SetInitializer(new EntitiesContextInitializer()); } 

Don't forget to add using System.Data.Entity; .....

like image 98
Damb Avatar answered Sep 19 '22 13:09

Damb


You must create custom database initializer derived for example from DropCreateDatabaseIfModelChanges and fill data in overriden Seed method. Then you must use Database.SetInitializer to set your new initializer when application starts. Here is example (from CTP5) used to create custom index in the database.

like image 22
Ladislav Mrnka Avatar answered Sep 22 '22 13:09

Ladislav Mrnka