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

asp.net-core c# entity-framework-core

Question

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
3
11/9/2017 11:23:07 AM

Popular Answer

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.

0
11/9/2017 9:45:00 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow