How to deal with database changes during development with EF Core?

ぐ巨炮叔叔 提交于 2019-12-22 10:56:02

问题


I am struggling with database changes during development with .NET Core and Entity Framework Core.

When I create new model and add it to dbContext, even with dbContext.Database.EnsureCreated(); in Startup.cs it does not create new tables.

It will create all tables only if I will do dotnet ef database drop before, but it is pretty annoying to have to seed db every time adding new model. creating more and more migration every time does not look like good idea to me...

Can someone suggest better way?


回答1:


Unfortunately it seems that EF core does not support automatic migrations like in EF 6. This means for every change you have to add a new migration. You can in your Startup => Configure add this code to automatically apply the latest migration but if i understand their documentation right you have to add a new migration for every change.

        using (var serviceScope app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetRequiredService<YourDbContext>();
            context.Database.Migrate();
        }

Ref: EntityFramework Core automatic migrations https://github.com/aspnet/EntityFrameworkCore/issues/6214

Also agree with @Nathan about creating the structure yourself with fluent api.

Hope this helps.



来源:https://stackoverflow.com/questions/47197262/how-to-deal-with-database-changes-during-development-with-ef-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!