![](/img/trans.png)
[英]Entity Framework code first INSERT statement conflicted with the FOREIGN KEY constraint
[英]The INSERT statement conflicted with the FOREIGN KEY constraint - Entity Framework
我对2个实体之间的关系有疑问;
我的课程如下:
public class Cis
{
[Key]
public int CisId { get; set; }
public int ProjectId { get; set; }
[ForeignKey("ProjectId")]
public virtual ICollection<Overhead> Overheads { get; set; }
}
public class Overhead
{
public int Id { get; set; }
public int ProjectId { get; set; }
public SecuredWorkType SecuredWorkType { get; set; }
public decimal? OverheadRecoveryTotalValue { get; set; }
public decimal? BudgetedFinancialResultValue { get; set; }
public decimal? SecuredValue { get; set; }
public decimal? OverheadRecoveryTotalPercentage { get; set; }
}
当我尝试保存带有多个开销的CIS对象时,出现以下错误
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Overhead_dbo.Cis_ProjectId". The conflict occurred in database "CisLatest", table "dbo.Cis", column 'CisId'.The statement has been terminated."
由于设计方面的考虑,我不能将CisId用作我的外键-当我使用CisId作为外键时,它可以正常工作。
我在这里看到了类似的问题和答案,尽管它们显示将父实体放在孩子中? 不确定在这种情况下是否适用
谢谢你的帮助。
尝试
public class Cis
{
[Key]
public int CisId { get; set; }
[ForeignKey("ProjectId")] //here
public int ProjectId { get; set; }
public virtual ICollection<Overhead> Overheads { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.