In our last tutorial, we looked at how to create custom database initializer in entity framework. One of the main use of creating the custom database Initializer is to seed the database with useful data. In this tutorial let us look at the how to seed database in Entity Framework
Table of Contents
Seed Database in Entity Framework
Seeding the database means pre-populating the database with default data. This is useful in scenarios where you want to provide some test data in the development environment. You could use this to set up the application for the first time in a production environment by providing the sample or useful master data.
You need to use the Custom database initializers to use the Seed method.
Using inbuilt Database Initializers
The Entity Framework built in initializers contains the seed method. This method actually does nothing. So you need to override the seed method with your own implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class EFContext :DbContext { public EFContext() : base("EFDatabase") { Database.SetInitializer(new EFInitializer()); } public DbSet Users {get; set; } } public class EFInitializer: DropCreateDatabaseAlways { protected override void Seed(EFContext context) { context.SaveChanges(); base.Seed(context); } } |
In the above example, our custom database initializer inherits from the built in initializer DropCreateDatabaseAlways. We then added seed method which overrides the base class’s seed method. We then added two users to the user’s collection. Finally, SaveChanges is called to persist the data to the database.
The seed method is called immediately after a new, empty database is created. Seed method is never called for an existing database. That is because the existing database might already have data in it.
Seeding Database using IDatabaseInitializer
The built in initialisers has already implemented the seed method and calls the method when InitializeDatabase is called. In case you want to create the custom initializer using the interface IDatabaseInitializer, then you need to write the seed method and call it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class EFInitializer : IDatabaseInitializer { public void InitializeDatabase(EFContext context) { //This will drop & Create the database, //which mimics the DropCreateDatabaseAlways behaviour context.Database.Delete(); context.Database.Create(); //Calling the Seed method Seed(context); } protected virtual void Seed(EFContext context) { /// Our seed code context.SaveChanges(); } } |
In the above example, we have created EFInitializer which implements the interface IDatabaseInitializer. The above code deletes and creates the database, which is what DropCreateDatabaseAlways built in initializer does. Seed method is called after the database is created. The method is similar to the previous example.
Seeding Data In Relationships
In Entity Framework, you can define relationships between your models. You can read about Relationships in Entity Framework. Consider you have model Role model along with user model. The User and Role model has a One to Many Relationship between them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class User { public int UserID { get; set; } public string Name { get; set; } public string Email { get; set; } Public int RoleID { Get ;set;} public virtual Role Role { get; set; } } Public Class Role { Public int RoleID { get ;set;} Public string name {get; set;} public virtual ICollection<User> Users { get; set; } } |
And our seed method will look like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | protected virtual void Seed(EFContext context) { /// Our seed code Role role= new Role() {Name="Admin"}); context.Roles.Add(role); role= new Role() {Name="Users"}); context.Roles.Add(role); context.SaveChanges(); } |
Conclusion
In this tutorial, we looked at the database seed method. We saw how to create seed method when using the Built-in Initializer. We also looked at how to create the same, while inheriting from the IDatabaseInitializer.
Source Code
You can download the source code from the GitHub repository