Lets talk strategy for a moment. By default the entity framework uses a database creation strategy called CreateDatabaseIfNotExists. As the name implies this database initializer only creates the database if it’s not on your SQL Express or by a connectionstring specified location.
Currently there are two more strategies to choose from; the somewhat harsh DropCreateDatabaseAlways and the more useful DropCreateDatabaseIfModelChanges. All these strategies have one big disadvantage in common. They trash your data relentlessly, so be careful what you ask for. To provide some initial data to play with while developing an model, we can override the Seed method of our custom strategy / database initializer.
In the example below we use the Code First model we’ve created in a previous example. We change the default database creation strategy with our custom database initializer derived from DropCreateDatabaseIfModelChanges. We also override the Seed method to initially fill the database with some useful data. So first we need to create our custom database initializer.
using System; using System.Collections.Generic; using System.Data.Entity; using CodeFirstPocos; namespace CodeFirstData { public class DropBomContextIfModelChanges : DropCreateDatabaseIfModelChanges<BomContext> { protected override void Seed(BomContext context) { var bom = new BillOfMaterials { CreationDate = new DateTime(2012, 5, 18), ProjectName = "The Code Project", AssemblyItems = new List<AssemblyItem> { new AssemblyItem { PartNumber = "A1284-e", Description = "Case", Price = 199m, Quantity = 1, }, new AssemblyItem { PartNumber = "124-448", Description = "Motherboard", Price = 398m, Quantity = 1, }, new AssemblyItem { PartNumber = "i7-2600K", Description = "Intel i7 CPU", Price = 720m, Quantity = 1, }, } }; context.BillOfMaterialsSet.Add(bom); base.Seed(context); } } }
To test this strategy we add a new boolean property to the BillOfMaterials poco named IsHandleWithCare.
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace CodeFirstPocos { public class BillOfMaterials { public int Id { get; set; } [MaxLength(24), Required, Column("Project")] public string ProjectName { get; set; } public DateTime CreationDate { get; set; } public List<AssemblyItem> AssemblyItems { get; set; } public bool IsHandleWithCare { get; set; } } }
We write a testmethod to take this custom database initializer for a spin. We call the Database.SetInitializer method to provide it with our custom initializer. If we start using the DbContext while our model has some changes it will recreate and seed our database.
using System; using System.Data.Entity; using CodeFirstData; using CodeFirstPocos; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace CodeFirstTests { [TestClass] public class BomTests { [TestMethod] public void ModelChangeShouldCreateAndSeedNewDatabase() { Database.SetInitializer(new DropBomContextIfModelChanges()); var bomContext = new BomContext(); var bom = new BillOfMaterials { CreationDate = DateTime.Today, ProjectName = "Important Project", IsHandleWithCare = true, }; bomContext.BillOfMaterialsSet.Add(bom); bomContext.SaveChanges(); } } }
The database is created with our new IsHandleWithCare column.
The database is also seeded with our seed data from the custom database initializer.


