简体   繁体   English

Microsoft.EntityFrameworkCore.SqlServer - 数据库未在 context.Database.Migrate() 上更新;

[英]Microsoft.EntityFrameworkCore.SqlServer - Database not updating on context.Database.Migrate();

I'm trying to move a project from using Dapper to Microsoft.EntityFrameworkCore.SqlServer.我正在尝试将一个项目从使用 Dapper 转移到 Microsoft.EntityFrameworkCore.SqlServer。

I've created the entities and their respective mappings(configurations).我已经创建了实体及其各自的映射(配置)。

Also I'm running in my ConfigureServices method inside startup.此外,我在启动时使用我的 ConfigureServices 方法运行。

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但是我添加了一些新的 DbSets 并且还更改了一些列类型

Am I missing something?我错过了什么吗?

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 https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx

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; Go到entity framework表,添加create migration的名称;
  5. Modify the database with the new tables/columns e etc;使用新表/列 e 等修改数据库;
  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;这样你就不需要注释代码的up部分;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft.EntityFrameworkCore.SqlServer依赖关系无法解决 - Microsoft.EntityFrameworkCore.SqlServer dependency could not be resolved 实体框架核心-如何访问Context.Database.Migrate() - Entity Framework Core - How To Access Context.Database.Migrate() 无法找到无法找到包“Microsoft.EntityFrameworkCore.SqlServer” - Unable to find Unable to find package 'Microsoft.EntityFrameworkCore.SqlServer' Nuget 找不到包(Microsoft.EntityFrameworkCore.SqlServer) - Nuget pacakage (Microsoft.EntityFrameworkCore.SqlServer) can not found 使用复合键在 Microsoft.EntityFrameworkCore.SqlServer 5.0.8 C# 中进行更新 - Upsert in Microsoft.EntityFrameworkCore.SqlServer 5.0.8 C# with a composite key NU1107:Microsoft.EntityFrameworkCore.SqlServer 的 .Net Core 2.1 兼容替代品? - NU1107: .Net Core 2.1-compatible alternatives to Microsoft.EntityFrameworkCore.SqlServer? 错误 NU1202 Microsoft.EntityFrameworkCore.SqlServer 5.0.0 package 与 net50 不兼容 - Error NU1202 Microsoft.EntityFrameworkCore.SqlServer 5.0.0 package is not compatible with net50 Microsoft.EntityFrameworkCore.Database.Connection[20004] - Microsoft.EntityFrameworkCore.Database.Connection[20004] 将实体框架生成的数据库迁移到sqlserver - Migrate database generated by entity framework to sqlserver 使用 Microsoft.EntityFrameworkCore.Sqlite 从 SQLite 数据库逆向工程 - Reverse engineering from SQLite database using Microsoft.EntityFrameworkCore.Sqlite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM