user11689404
user11689404

Reputation:

Add some data to table on first run in asp.net core(Fill database)

i'm creating a web site using Asp.net core and i want to add some records to some tables at first time i run my website (on host i mean) for example category names or any .

Does any know how can i do this ?

Upvotes: 1

Views: 1181

Answers (1)

Support Ukraine
Support Ukraine

Reputation: 1024

You can use one of the described approaches from this article.

public void Main(string[] args)
{
    var host = BuildWebHost(args);
    using (var scope = host.Services.CreateScope())
    {
        var services = scope.ServiceProvider;
        var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
        DataSeeder.SeedCountries(context);
    }
    host.Run();
}

Upvotes: 2

Related Questions