I've been using C# Bogus (aka Faker) to generate data with EF Core, and it's been absolutely invaluable for development.
I have this set up in my context class, in the OnModelCreating
method. This seems necessary to get the data into the database, since it uses the entity HasData
method:
long accountId = 1;
var accountData = new Faker<AccountModel>()
.RuleFor(m => m.Id, f => accountId++)
.RuleFor(m => m.Name, f => f.Company.CompanyName())
.Generate(6);
builder.Entity<AccountModel>().HasData(accountData);
However, the entire data generation script runs on each request made to the API.
Where can I put the Bogus scripts that will allow it to seed the database with dotnet ef migrations add init
, but without running on every request?