1

I'm trying to move a project from using Dapper to Microsoft.EntityFrameworkCore.SqlServer.

I've created the entities and their respective mappings(configurations).

Also I'm running in my ConfigureServices method inside startup.

using var scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<SpannerContext>();
context.Database.EnsureCreated();
context.Database.Migrate();

But nothing is happening;

Logs shows:

info: Microsoft.EntityFrameworkCore.Migrations[20405]
      No migrations were applied. The database is already up to date.

But I've added some new DbSets and also change some column types

Am I missing something?

Vinicius Andrade
  • 151
  • 1
  • 4
  • 22
  • context.Database.EnsureCreated() should create the database. Did you check the database? The Migrate() method is not effective unless you execute the command add-migration. Read more learn.microsoft.com/en-us/ef/core/managing-schemas/ensure-created – yousif Dec 07 '22 at 21:29
  • I've also tried to run EnsureCreated before Migrate, but no success – Vinicius Andrade Dec 07 '22 at 21:31
  • 1
    I think it's important to say that the database was already created. – Vinicius Andrade Dec 07 '22 at 21:34
  • Okay in that case, EnsureCreated is not effective. Are you using migrations? If you are not then you have to call EnsureDeleted then EnsureCreated (Which delete the database). If you don't want the database to be deleted, then you must use migrations. Run the command Add-migration *yourMessage*. Read more here learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/ – yousif Dec 07 '22 at 21:38
  • After adding the migration by using the command Add-migration, Database.Migrate() will act as if you were to excute the command update-database. which simply applies the created migration to the databse. – yousif Dec 07 '22 at 21:40
  • I've also tried to do this. It created the first migrations with all create tables command. When I tried to run the update database, it complained saying that the tables were already created. – Vinicius Andrade Dec 07 '22 at 21:41
  • Yes, this is expected behavior. That's because your database wasn't created using migrations. Here is my suggestion, if you are using source control, revert (or stash) the new changes you made to the database. So that it matches the current schema. Then add migration for creating all the database. Finally, add your new changes again and add a new migration. You should be able to apply the changes then. – yousif Dec 07 '22 at 21:47
  • I've done that to, but also when I tried to run the update database, it complained saying that the tables were already created. – Vinicius Andrade Dec 07 '22 at 21:59
  • My mistake. There is an extra step that needs to be done. You must comment the Up code in the first migration. Then execute update database, and finally add and apply the new changes. This is because EF tracks the applied migrations in the database with a Migration tables. Check this (but it is for EF6) https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/existing-database – yousif Dec 08 '22 at 05:41
  • Will I need to leave the up part commented forever? I'll try to do that asap – Vinicius Andrade Dec 08 '22 at 05:52
  • No. You should uncomment it afterward. Commenting is being done only because the database you are using wasn't created by migrations. When you comment it, you are basically telling EF core to consider the migration already applied. You should uncomment it, so future databases are created correctly with migrations. – yousif Dec 08 '22 at 06:47
  • Also, you will not need context.Database.EnsureCreated(); anymore. Because you are using migrations. Only use this method for testing databases that you don't care about the data inside. – yousif Dec 08 '22 at 06:48
  • The only way I made it work was: Scaffold db. Comment the up code from initial. Make changes, run database update. ```database.Migrate()``` doesn't do any changes – Vinicius Andrade Dec 19 '22 at 12:53

2 Answers2

0

try to generate your models and configuration form an existing database, then apply the modifications/changes in your models.

https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx

0

I order to solve the problem i followed the following steps

  1. Scaffold the database;
  2. Add a migration;
  3. Run the code for the first time, to ensure the entity framework migrations table is created;
  4. Go to the entity framework table and add the name of the create migration;
  5. Modify the database with the new tables/columns e etc;
  6. Add a new migration;

The new migration should work as expected;

This way you don't need to comment the up part of the code;

Vinicius Andrade
  • 151
  • 1
  • 4
  • 22