If you are trying to save test data during development, consider using the Seed method for IDatabaseInitializer (i.e. the database initializer). The Seed method is called during itialization (after applying any model changes) and fills in any test data that you specify.
The best way to specify which initializer you want to use EF is in the global.asax.cs file.
Here is an example:
[Global.asax.cs]
Database.SetInitializer<MyApplicationDbContext>(new MyApplicationDbContextInitializer());
[MyApplicationDbContextInitializer.cs]
public class MyApplicationDbContextInitializer : DropCreateDatabaseAlways<MyApplicationDbContext> { protected override void Seed(TitleDB context) { context.Products.Add(new Product { Id = 1, Name = "Foobar XL" }); context.Products.Add(new Product { Id = 2, Name = "Foobar LG" }); context.Products.Add(new Product { Id = 3, Name = "Foobar MD" }); context.SaveChanges(); base.Seed(context); } }
source share