- Entity Framework Tutorial
- Entity Framework - Home
- Entity Framework - Overview
- Entity Framework - Architecture
- Entity F - Environment Setup
- Entity Framework - Database Setup
- Entity Framework - Data Model
- Entity Framework - DbContext
- Entity Framework - Types
- Entity Framework - Relationships
- Entity Framework - Lifecycle
- Entity F - Code First Approach
- Entity F - Model First Approach
- Entity F - Database First Approach
- Entity Framework - DEV Approaches
- Entity F - Database Operations
- Entity Framework - Concurrency
- Entity Framework - Transaction
- Entity Framework - Views
- Entity Framework - Index
- Entity F - Stored Procedures
- Entity F - Disconnected Entities
- Entity F - Table-Valued Function
- Entity Framework - Native SQL
- Entity Framework - Enum Support
- Entity F - Asynchronous Query
- Entity Framework - Persistence
- Entity F - Projection Queries
- Entity F - Command Logging
- Entity F - Command Interception
- Entity Framework - Spatial Data Type
- Entity Framework - Inheritance
- Entity Framework - Migration
- Entity Framework - Eager Loading
- Entity Framework - Lazy Loading
- Entity Framework - Explicit Loading
- Entity Framework - Validation
- Entity Framework - Track Changes
- Entity Framework - Colored Entities
- Entity F - Code First Approach
- Entity Framework - First Example
- Entity Framework - Data Annotations
- Entity Framework - Fluent API
- Entity Framework - Seed Database
- Entity F - Code First Migration
- Entity F - Multiple DbContext
- Entity F - Nested Entity Types
- Entity Framework Resources
- Entity Framework - Quick Guide
- Entity Framework - Useful Resources
- Entity Framework - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Entity Framework - Seed Database
In Entity Framework, Seed was introduced in EF 4.1 and works with database initializers. The general idea of a Seed Method is to initialize 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 lists of known Students, Courses, etc. When the data is initialized, 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 initializer 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 UniDBInitializer<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 initialized. You need to set this DB initializer class in context class as shown in the following code.
public MyContext() : base("name=MyContextDB") { Database.SetInitializer<MyContext>(new UniDBInitializer<MyContext>()); }
Following is the complete class implementation of MyContext class, which also contains the DB initializer class.
public class MyContext : DbContext { public MyContext() : base("name=MyContextDB") { Database.SetInitializer<MyContext>(new UniDBInitializer<MyContext>()); } public virtual DbSet<Course> Courses { get; set; } public virtual DbSet<Enrollment> Enrollments { get; set; } public virtual DbSet<Student> Students { get; set; } private class UniDBInitializer<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.
We recommend that you execute the above example in a step-by-step manner for better understanding.