我遇到了两个实体之间关系的问题;
我的课程如下;
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."
由于设计方面的考虑,我无法在Cis上使用CisId作为我的外键 - 当我使用CisId作为外键时,它运行良好。
我在这里看到过类似的问题和答案,虽然他们表示将父实体放在孩子体内?在这种情况下不确定是否适用
感谢您的帮助。
答案 0 :(得分:0)
尝试
public class Cis
{
[Key]
public int CisId { get; set; }
[ForeignKey("ProjectId")] //here
public int ProjectId { get; set; }
public virtual ICollection<Overhead> Overheads { get; set; }
}