You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EF Core迁移后未生成业务表,提示无迁移应用求助

EF Core迁移执行后仅生成空_EFMigrationHistory表,业务表未创建的排查方案

执行context.Database.Migrate();后,数据库成功创建,但仅生成空的_EFMigrationHistory表,所有业务表都未创建,且提示无迁移应用成功。以下是相关代码及排查方向:

DbContext代码

public class CatalogDBContext : DbContext
{
    public DbSet<CatalogItem> CatalogItems { get; set; }
    public DbSet<CatalogBrand> CatalogBrands { get; set; }
    public DbSet<CatalogType> CatalogTypes { get; set; }

    public CatalogDBContext(DbContextOptions<CatalogDBContext> options) : base(options) 
    {
        DbInterception.Add(new CLoggerEFInterceptor());
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.ApplyConfiguration(new BrandEntityTypeConfiguration());
        builder.ApplyConfiguration(new TypeEntityTypeConfiguration());
        builder.ApplyConfiguration(new ItemEntityTypeConfiguration());
    }
}

Program初始化代码

using (var scope = app.Services.CreateScope())
{
    var context = scope.ServiceProvider.GetRequiredService<CatalogDBContext>();
    var env = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
    var settings = scope.ServiceProvider.GetRequiredService<IOptions<CatalogSettings>>();
    var logger = scope.ServiceProvider.GetRequiredService<ILogger<CatalogDBSeedData>>();

    context.Database.Migrate();

    new CatalogDBSeedData().SeedAsync(context, env, settings, logger).Wait();
}

InitialMigration迁移代码

public partial class InitialMigration : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateSequence(
            name: "brand_hilo",
            incrementBy: 10);

        migrationBuilder.CreateSequence(
            name: "item_hilo",
            incrementBy: 10);

        migrationBuilder.CreateSequence(
            name: "type_hilo",
            incrementBy: 10);

        migrationBuilder.CreateTable(
            name: "Brand",
            columns: table => new
            {
                Id = table.Column<long>(type: "bigint", nullable: false),
                Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Brand", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "Type",
            columns: table => new
            {
                Id = table.Column<long>(type: "bigint", nullable: false),
                Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Type", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "Item",
            columns: table => new
            {
                Id = table.Column<long>(type: "bigint", nullable: false),
                Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
                Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
                Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                PictureFileName = table.Column<string>(type: "nvarchar(max)", nullable: true),
                TypeId = table.Column<long>(type: "bigint", nullable: false),
                BrandId = table.Column<long>(type: "bigint", nullable: false),
                AvailableStock = table.Column<int>(type: "int", nullable: false),
                RestockThreshold = table.Column<int>(type: "int", nullable: false),
                MaxStockThreshold = table.Column<int>(type: "int", nullable: false),
                OnReorder = table.Column<bool>(type: "bit", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Item", x => x.Id);
                table.ForeignKey(
                    name: "FK_Item_Brand_BrandId",
                    column: x => x.BrandId,
                    principalTable: "Brand",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
                table.ForeignKey(
                    name: "FK_Item_Type_TypeId",
                    column: x => x.TypeId,
                    principalTable: "Type",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Item_BrandId",
            table: "Item",
            column: "BrandId");

        migrationBuilder.CreateIndex(
            name: "IX_Item_TypeId",
            table: "Item",
            column: "TypeId");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Item");

        migrationBuilder.DropTable(
            name: "Brand");

        migrationBuilder.DropTable(
            name: "Type");

        migrationBuilder.DropSequence(
            name: "brand_hilo");

        migrationBuilder.DropSequence(
            name: "item_hilo");

        migrationBuilder.DropSequence(
            name: "type_hilo");
    }
}

排查方向

  • 迁移文件加载验证:检查迁移文件所在项目是否被启动项目正确依赖,在Package Manager Console执行Get-Migrations,确认InitialMigration能被列出,未列出则说明迁移未被识别。
  • 连接字符串校验:确认CatalogDBContext使用的连接字符串指向生成空表的数据库,可在Migrate()前输出连接字符串验证目标库正确性。
  • 迁移历史记录检查:查看_EFMigrationHistory表的MigrationId和ProductVersion是否与本地迁移文件一致,若有异常记录,可删除该表后重新执行Migrate(),或用Update-Database -Migration InitialMigration指定迁移强制执行。
  • 实体配置一致性验证:核对BrandEntityTypeConfiguration等配置类中的表名、属性定义,是否与迁移文件中的Brand/Type/Item表结构完全匹配,配置冲突会导致迁移静默失败。
  • 拦截器影响排查:暂时注释DbInterception.Add(new CLoggerEFInterceptor());,验证是否是拦截器干扰了迁移流程;添加日志输出查看EF Core迁移执行的详细日志,排查隐藏异常。
  • 命令行迁移验证:执行dotnet ef database update,查看更详细的执行日志,定位代码中Migrate()未输出的错误信息。

内容的提问来源于stack exchange,提问作者Vlad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 10:18:19