【发布时间】:2021-12-12 21:03:59
【问题描述】:
我正在尝试使用 ForeignKey 更新数据库表。
在 Departures 中,我有“AirportCode”列。 在航班内,我有“DepartureAirportCode”列。 我试图将 DepartureAirportCode 添加为 ForeignKey。
我收到了这个错误。
失败:
Microsoft.EntityFrameworkCore.Database.Command[20102] 执行 DbCommand 失败 (213ms) [Parameters=[], CommandType='Text', CommandTimeout='300'] ALTER TABLE "Flight" ADD CONSTRAINT "FK_Flight_Departures_DepartureAirportCode" FOREIGN KEY ("DepartureAirportCode") REFERENCES "Departures" ("AirportCode") ON DELETE CASCADE; 执行 DbCommand 失败 (213ms) [Parameters=[], CommandType='Text', CommandTimeout='300'] ALTER TABLE "Flight" ADD CONSTRAINT "FK_Flight_Departures_DepartureAirportCode" FOREIGN KEY ("DepartureAirportCode") REFERENCES "Departures" ("AirportCode") ON DELETE CASCADE; Npgsql.PostgresException (0x80004005): 23503: 在表“Flight”上插入或更新违反了外键约束“FK_Flight_Departures_DepartureAirportCode”
消息文本:
在表“Flight”上插入或更新违反了外键约束“FK_Flight_Departures_DepartureAirportCode” 详细信息:键 (DepartureAirportCode)=() 不在“出发”表中。
更新:解决了这个问题。我的数据库出发表数据的问题。谢谢
【问题讨论】:
-
如果您需要帮助,您必须发布您的课程
-
我已经更新了课程。
-
嗨@hanushic,请分享你的模型类,如果你在dbcontext中配置了任何关系。另外,请分享一下你操作数据库的代码是什么。
标签: postgresql asp.net-core entity-framework-core ef-code-first