Seed Databases in EntityFramework

When we begin working on an application, we spend most of the time creating a database and tables, but in Entity Framework, there is the advantage that it can automatically create, update and drop databases when the application runs.
 
EF suggests using "Database Migrations" to prevent losing databases or records. Usually, we do not use "Database Migrations" in our normal application and for this EF we have the database "Seed" method that allows you to seed some dummy data in the database for testing purposes and this could be used in our applications such as MVC, ASP.NET, Windows Forms apps and so on.
 
To seed data into a database, you need to create a custom DB initializer as in the following:
  • DropCreateDatabaseIfModelChanges<DBContext>
  • CreateDatabaseIfNotExists<DBContext>
  • DropCreateDatabaseAlways<DBContext>
Now we will create an application with a seed method.
 
Step 1
 
Create a new project. Open Visual Studio and click New Project, then select Visual C# and create WindowsFormsApplication1.
 
Step 2
 
Install Entity Framework. The process of installing Entity Framework is shown in my previous tutorial “Nuget Package Manager”.
 
 
Step 3
 
Add a class for Students to define the parameters.
 
Right-click on the Solution Explorer and add a class and define an attribute in this class using the Code First approach.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace WindowsFormsApplication1  
  7. {  
  8.    class Student  
  9.    {  
  10.       public int StudentId { getset; }  
  11.       public string FirstName { getset; }  
  12.       public string LastName { getset; }  
  13.    }  
 
Step 4
 
Add another class for StudentEntity to define the DbContext.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data.Entity;  
  7. namespace WindowsFormsApplication1  
  8. {  
  9.    class studentEntity : DbContext  
  10.    {  
  11.       public DbSet<Student> Students { getset; }  
  12.    }  
Here we must remember that we need to a "using System.Data.Entity;" as in the following:
 
 
Step 5
 
Database Initializer.
 
Add a new class for Database initialize and now put the data for showing when the database was first created.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data.Entity;  
  7. namespace WindowsFormsApplication1   
  8. {  
  9.     class StudentDataBaseInitializer: DropCreateDatabaseIfModelChanges < studentEntity > {  
  10.         protected override void Seed(studentEntity context)   
  11.         {  
  12.             var students = new List < Student >   
  13.             {  
  14.                 new Student   
  15.                 {  
  16.                     FirstName = "Munesh", LastName = "Sharma"  
  17.                 },  
  18.                 new Student   
  19.                 {  
  20.                     FirstName = "Vinod", LastName = "Roy"  
  21.                 },  
  22.                 new Student   
  23.                 {  
  24.                     FirstName = "Manish ", LastName = "Kumar"  
  25.                 },  
  26.                 new Student   
  27.                 {  
  28.                     FirstName = "Rohit ", LastName = "jash"  
  29.                 },  
  30.                 new Student   
  31.                 {  
  32.                     FirstName = "gautam", LastName = "shree"  
  33.                 }  
  34.             };  
  35.             students.ForEach(s = > context.Students.Add(s));  
  36.             context.SaveChanges();  
  37.         }  
  38.     }  
code
 
Step 6
 
Seed data into the database. We use the following line for seeding the data into the database:
  1. Database.SetInitializer<studentEntity>(new StudentDataBaseInitializer()); 
Step 7
 
Display in the form. Add a form and use a button and then write the following code on button click.
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Data.Entity;  
  11. namespace WindowsFormsApplication1   
  12. {  
  13.     public partial class Form1: Form   
  14.     {  
  15.         public Form1()   
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void button1_Click(object sender, EventArgs e)   
  20.         {  
  21.             using(var context = new studentEntity())   
  22.             {  
  23.                 Database.SetInitializer < studentEntity > (new StudentDataBaseInitializer());  
  24.                 //This is linq query  
  25.                 var stdInfo = (from C in context.Students  
  26.                 select new   
  27.                 {  
  28.                     Id = C.StudentId, FirstName = C.FirstName  
  29.                 });  
  30.                 foreach(var info in stdInfo)   
  31.                 {  
  32.                     Console.WriteLine("ID : " + info.Id + ", Name : " + info.FirstName);  
  33.                 }  
  34.                 Console.ReadKey();  
  35.             }  
  36.         }  
  37.     }  
 
After clicking on the button you will see the result with FirstName and LastName.
 
You can view the same article on my blog.


Recommended Free Ebook
Similar Articles