EF Core/PostgreSQL迁移报错:关系'Logs'不存在
EF Core Code-First迁移PostgreSQL时反复抛出"Logs表不存在"错误
采用Code-First方式向本地PostgreSQL服务器执行迁移,LogEntity与TenantEntity的实现方式完全一致,在ApplicationDBContext中的配置也无差异。
执行以下命令:
dotnet ef migrations add {migrationName} dotnet ef database update
时会反复抛出错误:
relation "Logs" does not exist
补充说明:迁移文件中包含创建Logs表的代码,且Logs表最终确实会被创建,但迁移命令会先抛出该错误20-30次。
相关代码
InitialCreate迁移文件
public partial class InitialCreate : Migration { /// <inheritdoc /> protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "Logs", columns: table => new { Id = table.Column<string>(type: "text", nullable: false), Message = table.Column<string>(type: "text", nullable: false), Date = table.Column<DateTime>(type: "timestamp without time zone", nullable: false), LogLevel = table.Column<int>(type: "integer", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Logs", x => x.Id); }); migrationBuilder.CreateTable( name: "Tenants", columns: table => new { Id = table.Column<string>(type: "text", nullable: false), Name = table.Column<string>(type: "text", nullable: true) }, constraints: table => { table.PrimaryKey("PK_Tenants", x => x.Id); }); } /// <inheritdoc /> protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "Logs"); migrationBuilder.DropTable( name: "Tenants"); } }
ApplicationDBContext代码
internal class ApplicationDBContext : DbContext { public ApplicationDBContext(DbContextOptions dbContextOptions) : base(dbContextOptions) { } public DbSet<LogEntity> Logs { get; set; } public DbSet<TenantEntity> Tenants { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { new LogEntityConfiguration().Configure(modelBuilder.Entity<LogEntity>()); new TenantEntityConfiguration().Configure(modelBuilder.Entity<TenantEntity>()); } }
实体配置类
internal class TenantEntityConfiguration : IEntityTypeConfiguration<TenantEntity> { public void Configure(EntityTypeBuilder<TenantEntity> builder) { builder.HasKey(e => e.Id); } } internal class LogEntityConfiguration : IEntityTypeConfiguration<LogEntity> { public void Configure(EntityTypeBuilder<LogEntity> builder) { builder.HasKey(e => e.Id); } }
实体类代码
public class LogEntity { public string Id { get; set; } public string Message { get; set; } public DateTime Date { get; set; } public int LogLevel { get; set; } } public class TenantEntity { public string? Id { get; set; } public string? Name { get; set; } }
内容的提问来源于stack exchange,提问作者Chris Phillips
相关产品推荐
相关产品推荐

