DbMigrations in .NET with Code First: How to Repeat Statements with Expressions

My project uses ASP.NET MVC4, C #, EF Code First and the Faker-cs package ( https://github.com/oriches/faker-cs ). I wrote this example to test how Faker-cs works in my Migrations\Configuration.cs file:

 protected override void Seed(MyProject.Models.MyProjectContext context) { context.Companies.AddOrUpdate( p => p.Name, new Company { Name = Faker.Company.Name() } ); } 

How can I repeat n object generation time?

 protected override void Seed(MyProject.Models.MyProjectContext context) { context.Companies.AddOrUpdate( p => p.Name, // Repeat insertion of new Companies (ie, 10) ); } 
0
source share
1 answer

Using the lambda LINQ expression, something like this should work:

 protected override void Seed(Fideli100.Models.Fideli100Context context) { context.Companies.AddOrUpdate( p => p.Name, Enumerable.Range(1, 10). Select( x => new Company { Name = Faker.Company.Name() }).ToArray() ); } 
+1
source

Source: https://habr.com/ru/post/1490200/


All Articles