当前位置: 首页 > article >正文

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

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

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

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’。

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


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));忽略掉这个错误,因为这个错误其实只是一个警告,但是新版本直接会报错。


http://www.kler.cn/a/430546.html

相关文章:

  • MySQL外键类型与应用场景总结:优缺点一目了然
  • 详细讲解axios封装与api接口封装管理
  • 表达式语句、复合语句和空语句
  • 基础12 友元相关
  • 人工智能的视觉天赋:一文读懂卷积神经网络
  • Edge Scdn是用来干什么的?
  • 【前端】浏览器输入url到页面呈现发生了什么?
  • csrf漏洞复现
  • Copilot for Microsoft 365 Plugins 示例项目教程
  • FM25V20A-DGQ:耐用、快速、低功耗的F-RAM
  • kcat - Apache Kafka producer and consumer tool
  • 调度系统:基于 Couchbase 构建数仓 Temporal、Apache Airflow 和 DonpinScheduler 的详细比较
  • IdentityServer4框架、ASP.NET core Identity
  • ios使用UIScrollView和PageControl创建图片轮播
  • selenium学习:等待方式
  • 网络安全法-网络运行安全
  • Scala正则表达式
  • UAC2.0 speaker——带反馈端点的 USB speaker(16bit 单声道)
  • 大数据新视界 -- Hive 临时表与视图的应用场景(下)(30 / 30)
  • 机器学习 (西瓜书) 内容概要【不含数学推导】
  • MySQL 通过 Next-Key Locking 技术避免幻读问题
  • 【JavaEE】多线程(7)
  • python如何与前端交互
  • NFT Insider #158:$SAND 和 LAND 价格反弹
  • 【 C++ 入门基础】 —— 双壁传奇C语言和C++的爱恨情仇
  • PostGIS分区表学习相关