EFCore PostgreSQL在.NET9生成迁移文件错误

EFCore PostgreSQL在.NET9生成迁移文件错误

如果在将.net 9的项目生成的迁移文件应用到数据库的时候出现

csharp 复制代码
System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'SiteDbContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
   at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition`1.Log[TLoggerCategory](IDiagnosticsLogger`1 logger, TParam arg)
   at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.PendingModelChangesWarning(IDiagnosticsLogger`1 diagnostics, Type contextType)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal.NpgsqlMigrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'SiteDbContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.

中文翻译错误就是:

警告"Microsoft.EntityFrameworkCore.Migrations"产生错误。PendingModelChangesWarning':上下文'SiteDbContext'的模型有挂起的更改。在更新数据库之前添加一个新的迁移。此异常可以通过传递事件ID 'RelationalEventId来抑制或记录。PendingModelChangesWarning'到'DbContext '中的'ConfigureWarnings'方法。onconfigurations '或'AddDbContext'。

当出现这个错误的时候其实我们可以通过配置日志进行忽略,下面是代码,

csharp 复制代码
public class SiteDbContext(DbContextOptions<SiteDbContext> options) : DbContext(options)
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(SiteDbContext).Assembly);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        
        optionsBuilder.ConfigureWarnings(warnings =>
            warnings.Ignore(RelationalEventId.PendingModelChangesWarning));
    }
}

主要是 optionsBuilder.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning));忽略掉这个错误,因为这个错误其实只是一个警告,但是新版本直接会报错。

相关推荐
jikuaidi6yuan几秒前
并行口的基本概念
数据库·mongodb
web1368856587126 分钟前
Spring Boot 中使用 @Transactional 注解配置事务管理
数据库·spring boot·sql
小码编匠1 小时前
C#上位机实现高效示波器功能
后端·c#·.net
骷大人1 小时前
mysql开启配置binlog
数据库·mysql
Yolo_nn2 小时前
MySQL_第14章_存储过程与函数
数据库·mysql·存储过程·函数
GDDGHS_2 小时前
Flink自定义数据源
大数据·数据库·flink
老牛源码2 小时前
2290 ASP.NET+SQL+LW+基于.NET旅游网站系统的开发与实现 源码 配置 文档
sql·asp.net·.net·旅游
Milk夜雨2 小时前
数据库进阶教程:结合编程实现动态数据操作
数据库·python·adb
weisian1512 小时前
Redis篇-5--原理篇4--Lua脚本
数据库·redis·lua
是十一月末2 小时前
Linux的基本功能和命令
linux·服务器·开发语言·数据库