English 中文(简体)
Entity Framework Tutorial

Entity Framework Resources

Selected Reading

Entity Framework - Seed Database
  • 时间:2024-11-03

Entity Framework - Seed Database


Previous Page Next Page  

In Entity Framework, Seed was introduced in EF 4.1 and works with database initiapzers. The general idea of a Seed Method is to initiapze data into a database that is being created by Code First or evolved by Migrations. This data is often test data, but may also be reference data such as psts of known Students, Courses, etc. When the data is initiapzed, it does the following −

    Checks whether or not the target database already exists.

    If it does, then the current Code First model is compared with the model stored in metadata in the database.

    The database is dropped if the current model does not match the model in the database.

    The database is created if it was dropped or didn’t exist in the first place.

    If the database was created, then the initiapzer Seed method is called.

The Seed method takes the database context object as an input parameter, and the code in the method uses that object to add new entities to the database. To seed data into your database, you need to override the Seed method. Let’s take a look at the following example in which some of the default data are initiated into the database in an internal class.

private class UniDBInitiapzer<T> : DropCreateDatabaseAlways<MyContext> {

   protected override void Seed(MyContext context) {

      IList<Student> students = new List<Student>();

      students.Add(new Student() {
         FirstMidName = "Andrew", 
         LastName = "Peters", 
         EnrollmentDate = DateTime.Parse(DateTime.Today.ToString())
      });

      students.Add(new Student() {
         FirstMidName = "Brice", 
         LastName = "Lambson", 
         EnrollmentDate = DateTime.Parse(DateTime.Today.ToString())
      });

      students.Add(new Student() {
         FirstMidName = "Rowan", 
         LastName = "Miller", 
         EnrollmentDate = DateTime.Parse(DateTime.Today.ToString())
      });

      foreach (Student student in students)
      context.Students.Add(student);
      base.Seed(context);
   }
}

In the above code, student table is initiapzed. You need to set this DB initiapzer class in context class as shown in the following code.

pubpc MyContext() : base("name=MyContextDB") {
   Database.SetInitiapzer<MyContext>(new UniDBInitiapzer<MyContext>());
}

Following is the complete class implementation of MyContext class, which also contains the DB initiapzer class.

pubpc class MyContext : DbContext {

   pubpc MyContext() : base("name=MyContextDB") {
      Database.SetInitiapzer<MyContext>(new UniDBInitiapzer<MyContext>());
   }

   pubpc virtual DbSet<Course> Courses { get; set; }
   pubpc virtual DbSet<Enrollment> Enrollments { get; set; }
   pubpc virtual DbSet<Student> Students { get; set; }
	
   private class UniDBInitiapzer<T> : DropCreateDatabaseAlways<MyContext> {

      protected override void Seed(MyContext context) {

         IList<Student> students = new List<Student>();
			
         students.Add(new Student() {
            FirstMidName = "Andrew", 
            LastName = "Peters", 
            EnrollmentDate = DateTime.Parse(DateTime.Today.ToString()) 
         });

         students.Add(new Student() {
            FirstMidName = "Brice", 
            LastName = "Lambson", 
            EnrollmentDate = DateTime.Parse(DateTime.Today.ToString())
         });

         students.Add(new Student() {
            FirstMidName = "Rowan", 
            LastName = "Miller", 
            EnrollmentDate = DateTime.Parse(DateTime.Today.ToString())
         });

         foreach (Student student in students)
         context.Students.Add(student);
         base.Seed(context);
      }
   } 
}

When the above example is compiled and executed, you can see the data in a database as shown in the following image.

Data In Database

We recommend that you execute the above example in a step-by-step manner for better understanding.

Advertisements