- Entity F - Nested Entity Types
- Entity F - Multiple DbContext
- Entity F - Code First Migration
- Entity Framework - Seed Database
- Entity Framework - Fluent API
- Entity Framework - Data Annotations
- Entity Framework - First Example
- Entity F - Code First Approach
- Entity Framework - Colored Entities
- Entity Framework - Track Changes
- Entity Framework - Validation
- Entity Framework - Explicit Loading
- Entity Framework - Lazy Loading
- Entity Framework - Eager Loading
- Entity Framework - Migration
- Entity Framework - Inheritance
- Entity Framework - Spatial Data Type
- Entity F - Command Interception
- Entity F - Command Logging
- Entity F - Projection Queries
- Entity Framework - Persistence
- Entity F - Asynchronous Query
- Entity Framework - Enum Support
- Entity Framework - Native SQL
- Entity F - Table-Valued Function
- Entity F - Disconnected Entities
- Entity F - Stored Procedures
- Entity Framework - Index
- Entity Framework - Views
- Entity Framework - Transaction
- Entity Framework - Concurrency
- Entity F - Database Operations
- Entity Framework - DEV Approaches
- Entity F - Database First Approach
- Entity F - Model First Approach
- Entity F - Code First Approach
- Entity Framework - Lifecycle
- Entity Framework - Relationships
- Entity Framework - Types
- Entity Framework - DbContext
- Entity Framework - Data Model
- Entity Framework - Database Setup
- Entity F - Environment Setup
- Entity Framework - Architecture
- Entity Framework - Overview
- Entity Framework - Home
Entity Framework Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Entity Framework - Seed Database
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.
We recommend that you execute the above example in a step-by-step manner for better understanding.
Advertisements