You can insert data into your database tables in database initialization process. This will be important if you want to provide some test data for your application or to provide some default master data for your application.
To seed data into your database, you have to create custom DB initializer as you created in the previous chapter and override Seed method. Following example show how you can provide default data for Standard table while initializing the School database:
public class SchoolDBInitializer : DropCreateDatabaseAlways<SchoolDBContext> { protected override void Seed(SchoolDBContext context) { IList<Standard> defaultStandards = new List<Standard>(); defaultStandards.Add(new Standard() { StandardName = "Standard 1", Description = "First Standard" }); defaultStandards.Add(new Standard() { StandardName = "Standard 2", Description = "Second Standard" }); defaultStandards.Add(new Standard() { StandardName = "Standard 3", Description = "Third Standard" }); foreach (Standard std in defaultStandards) context.Standards.Add(std); //All standards will base.Seed(context); } }
No comments:
Post a Comment