3

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?

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Marek Urbanowicz
  • 12,659
  • 16
  • 62
  • 87
  • 4
    Code first is a gimmick that causes more problems than it solves. Create your database structure yourself, as you require it - and then describe it to EF using the fluent api. You'll be a happier person in the long run. :) – Nathan Nov 09 '17 at 08:37
  • 1
    Agree with @Nathan (we use a Schema Compare Tool to script the stuff) – gsharp Nov 09 '17 at 08:41
  • 1
    Well, the EFC way of handling this is indeed migrations. But it's not automatic. You need to run commands to add migration, update database etc. every time. – Ivan Stoev Nov 09 '17 at 08:49
  • 1
    Don't you think it's quite stupid for dev? I agree migrations are must have for production, but for dev we should be able to easily migrate like with hibernate in Java... Nothing to do, just run app and it adjust everything as much as it can. – Marek Urbanowicz Nov 09 '17 at 09:45
  • 2
    There's nothing wrong with Code First, as long as you understand what it's doing. The problem is that people often use it as a substitute for understanding SQL, and that's where you run into issues. Migrations may seem pointless during initial development, before your first commit, but after that, they are absolutely crucial for coordinating further database development between different developers. – Chris Pratt Nov 09 '17 at 14:16
  • I totally agree and I mentioned above that for production it's perfect solution... but how you deal with migrations during development...? – Marek Urbanowicz Nov 09 '17 at 14:17
  • 1
    Please vote for and share your ideas for fixing this workflow on issue [#3053](https://github.com/aspnet/EntityFrameworkCore/issues/3053)! – bricelam Nov 09 '17 at 17:13
  • I know is a old post, but still the "issue" - maybe this might help https://stackoverflow.com/questions/39526595/entityframework-core-automatic-migrations/61415487#61415487 – Ionut N Apr 24 '20 at 19:08

1 Answers1

0

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.