[英]Entity Framework Core - Failed migration with Npgsql provider
嘗試使用Entity Framework Core 2.0.1和提供程序npgsql 2.0.1從其模型創建數據庫時出錯。
錯誤描述是:
“約束«FK_PiezasStockExterno_ContenedoresDocumentos_IdContenedorDocume»關系«PiezasStockExterno»已經存在”。
我沒有調用Database.EnsureCreated()因為我知道它在遷移時會引起麻煩,並且數據庫在被丟棄之前所以我確保它不存在。 它使用以下命令或調用Database.EnsureCreated()。 什么可能是真正的問題?
錯誤腳本:
CREATE TABLE "public"."PiezasStockExterno"
(
"Id" serial NOT NULL,
"IdContenedorDocumentosPieza" int4 NULL,
"IdContenedorDocumentosVehiculo" int4 NULL,
CONSTRAINT "PK_PiezasStockExterno" PRIMARY KEY ("Id"),
CONSTRAINT "FK_PiezasStockExterno_ContenedoresDocumentos_IdContenedorDocumentosPieza"
FOREIGN KEY ("IdContenedorDocumentosPieza")
REFERENCES "public"."ContenedoresDocumentos" ("Id") ON DELETE RESTRICT,
CONSTRAINT "FK_PiezasStockExterno_ContenedoresDocumentos_IdContenedorDocumentosVehiculo"
FOREIGN KEY ("IdContenedorDocumentosVehiculo")
REFERENCES "public"."ContenedoresDocumentos" ("Id") ON DELETE RESTRICT
)
楷模:
[Table("PiezasStockExterno", Schema = "public")]
public class PiezaStockExterno
{
[Key]
public int Id { get; set; }
public int? IdContenedorDocumentosPieza { get; set; }
[ForeignKey("IdContenedorDocumentosPieza")]
public virtual ContenedorDocumentos ContenedorDocumentosPieza { get; set; }
public int? IdContenedorDocumentosVehiculo { get; set; }
[ForeignKey("IdContenedorDocumentosVehiculo")]
public virtual ContenedorDocumentos ContenedorDocumentosVehiculo { get; set; }
}
[Table("ContenedoresDocumentos", Schema = "public")]
public class ContenedorDocumentos
{
[Key]
public int Id { get; set; }
[InverseProperty("ContenedorDocumentos")]
public IList<Imagen> Imagenes { get; set; }
[InverseProperty("ContenedorDocumentos")]
public IList<Foto> Fotos { get; set; }
[InverseProperty("ContenedorDocumentos")]
public IList<Documento> Documentos { get; set; }
[InverseProperty("ContenedorDocumentos")]
public IList<Video> Videos { get; set; }
}
語境:
public NContext(DbContextOptions<NContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.UseNpgsql(ConnectionString, b => b.MigrationsAssembly("WebAPI"));
optionsBuilder.EnableSensitiveDataLogging();
base.OnConfiguring(optionsBuilder);
}
WebAPI項目中的Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkNpgsql().AddDbContext<Infrastructure.Data.NContext>();
services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
.AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
AutoMapperConfig.Initialize();
}
命令:
dotnet ef migrations add InitialMigration
dotnet ef database update
OR
Database.EnsureCreated()
InitialMigration.cs:
migrationBuilder.CreateTable(
name: "PiezasStockExterno",
schema: "public",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn),
IdContenedorDocumentosPieza = table.Column<int>(nullable: true),
IdContenedorDocumentosVehiculo = table.Column<int>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_PiezasStockExterno", x => x.Id);
table.ForeignKey(
name: "FK_PiezasStockExterno_ContenedoresDocumentos_IdContenedorDocumentosPieza",
column: x => x.IdContenedorDocumentosPieza,
principalSchema: "public",
principalTable: "ContenedoresDocumentos",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_PiezasStockExterno_ContenedoresDocumentos_IdContenedorDocumentosVehiculo",
column: x => x.IdContenedorDocumentosVehiculo,
principalSchema: "public",
principalTable: "ContenedoresDocumentos",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
你絕對會使用FK約束名稱命中63字節的PostgreSQL最大標識符長度 ,這在截斷后變為一個相同,因此混淆已經存在錯誤(盡管可以看出該名稱被截斷)。
由於目前只能使用Fluent API指定FK約束名稱,因此需要覆蓋OnModelCreating
並添加以下代碼(使用對您有意義且不超過63個字符的任何名稱):
modelBuilder.Entity<PiezaStockExterno>()
.HasOne(e => e.ContenedorDocumentosPieza)
.WithMany()
.HasConstraintName("FK_PiezasStockExterno_IdContenedorDocumentosPieza");
modelBuilder.Entity<PiezaStockExterno>()
.HasOne(e => e.ContenedorDocumentosVehiculo)
.WithMany()
.HasConstraintName("FK_PiezasStockExterno_IdContenedorDocumentosVehiculo");
如果您添加逆集合導航屬性,請不要忘記更新相應的WithMany
調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.