How do you configure the DbContext when creating Migrations in Entity Framework Core?

后端 未结 7 1298
时光取名叫无心
时光取名叫无心 2020-12-11 02:51

Is there way that dependency injection can be configured/bootstrapped when using Entity Framework\'s migration commands?

Entity Framework Core supports dependency in

7条回答
  •  鱼传尺愫
    2020-12-11 03:10

    I just ask for an instance and run migrations in my Startup.cs file

      public void ConfigureServices(IServiceCollection services)
        {
            // ASPNet Core Identity
            services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("RRIdentityConnectionString")));
    
         }
    

    And then in Configure:

       public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            var rrIdentityContext = app.ApplicationServices.GetService();
            rrIdentityContext.Database.Migrate();
        }
    

    Note: There is no 'EnsureCreated' for the database. Migrate is supposed to create it if it doesn't exist, although how it is supposed to figure out the permissions I don't know - so I created an empty database.

提交回复
热议问题