auto create database in Entity Framework Core

前端 未结 5 729
南笙
南笙 2020-11-29 17:43

My application which is being ported to .NET core will use the new EF Core with SQLite. I want to automatically create the database and table structures when the app is firs

相关标签:
5条回答
  • 2020-11-29 18:06

    For EF Core 2.0+ I had to take a different approach because they changed the API. As of March 2019 Microsoft recommends you put your database migration code in your application entry class but outside of the WebHost build code.

    public class Program
    {
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();
            using (var serviceScope = host.Services.CreateScope())
            {
                var context = serviceScope.ServiceProvider.GetRequiredService<PersonContext>();
                context.Database.Migrate();
            }
            host.Run();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
    
    0 讨论(0)
  • 2020-11-29 18:08

    If you have created the migrations, you could execute them in the Startup.cs as follows.

     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
     {
          using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
          {
                var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
                context.Database.Migrate();
          }
    
          ...
    

    This will create the database and the tables using your added migrations.

    If your not using Entity Framework Migrations, and instead just need your DbContext model created exactly as it is in your context class at first run, then you can use:

     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
     {
          using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
          {
                var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
                context.Database.EnsureCreated();
          }
    
          ...
    

    Instead.

    If you need to delete your database prior to making sure it's created, call:

                context.Database.EnsureDeleted();
    

    Just before you call EnsureCreated()

    Adapted from: http://docs.identityserver.io/en/latest/quickstarts/7_entity_framework.html?highlight=entity

    0 讨论(0)
  • 2020-11-29 18:11

    If you get the context via the parameter list of Configure in Startup.cs, You can instead do this:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env,  LoggerFactory loggerFactory,
        ApplicationDbContext context)
     {
          context.Database.Migrate();
          ...
    
    0 讨论(0)
  • 2020-11-29 18:15

    My answer is very similar to Ricardo's answer, but I feel that my approach is a little more straightforward simply because there is so much going on in his using function that I'm not even sure how exactly it works on a lower level.

    So for those who want a simple and clean solution that creates a database for you where you know exactly what is happening under the hood, this is for you:

    public Startup(IHostingEnvironment env)
    {
        using (var client = new TargetsContext())
        {
            client.Database.EnsureCreated();
        }
    }
    

    This pretty much means that within the DbContext that you created (in this case, mine is called TargetsContext), you can use an instance of the DbContext to ensure that the tables defined with in the class are created when Startup.cs is run in your application.

    0 讨论(0)
  • 2020-11-29 18:28

    If you haven't created migrations, there are 2 options

    1.create the database and tables from application Main:

    var context = services.GetRequiredService<YourRepository>();
    context.Database.EnsureCreated();
    

    2.create the tables if the database already exists:

    var context = services.GetRequiredService<YourRepository>();
    context.Database.EnsureCreated();
    RelationalDatabaseCreator databaseCreator =
    (RelationalDatabaseCreator)context.Database.GetService<IDatabaseCreator>();
    databaseCreator.CreateTables();
    

    Thanks to Bubi's answer

    0 讨论(0)
提交回复
热议问题