In this example, I am using a project that uses MVC4, EF Code First, and Automatic Migrations. Therefore, if you use the same thing, your Migrations\Configuration.cs file should look like this:
internal sealed class Configuration : DbMigrationsConfiguration<MyProject.Models.MyProjectContext> { public Configuration() { AutomaticMigrationsEnabled = true; } ...
For individual elements, the example is trivial:
protected override void Seed(MyProject.Models.MyProjectContext context) { context.Customers.AddOrUpdate( c => c.Name, new Customer { Name = Faker.Name.FullName() } ); context.SaveChanges(); ...
For a certain number of elements, I liked the idea of โโusing a lambda expression, like Factory Girl (for Ruby on Rails). In another question I made ( DbMigrations in .NET w / Code First: how to repeat statements with expressions), the answer uses Enumberable.Range() to indicate the number of elements:
protected override void Seed(MyProject.Models.MyProjectContext context) { context.Companies.AddOrUpdate( p => p.Name, Enumerable.Range(1, 10). Select( x => new Company { Name = Faker.Company.Name() }).ToArray() ); context.SaveChanges(); } ...
source share