我有一个带有C#和.NET Framework 4.7的Entity Framework 6.1.3 Code First数据访问库。
我有这两个表:
public class AggregationChildren
{
public int AggregationChildrenId { get; set; }
public int AggregationId { get; set; }
public int Position { get; set; }
public virtual Aggregation Aggregation { get; set; }
public virtual Code Code { get; set; }
}
public class Code
{
public int CodeId { get; set; }
public string Serial { get; set; }
[ ... ]
public virtual AggregationChildren AggregationChild { get; set; }
[ ... ]
}
及其配置文件:
class AggregationChildrenConfiguration : EntityTypeConfiguration<AggregationChildren>
{
public AggregationChildrenConfiguration()
{
HasKey(ag_ch => ag_ch.AggregationChildrenId);
HasRequired(ag_ch => ag_ch.Aggregation)
.WithMany(ag => ag.AggregationChildren)
.HasForeignKey(ag_ch => ag_ch.AggregationId);
HasRequired(ag_ch => ag_ch.Code)
.WithOptional(c => c.AggregationChild)
.WillCascadeOnDelete(false);
}
}
class CodeConfiguration : EntityTypeConfiguration<Code>
{
public CodeConfiguration()
{
HasKey(c => c.CodeId);
[ ... ]
}
}
我试图在此处设置如果AggregationChildren
中有数据AggregationChildrenId
表中必须有相同的数据(CodeId
== Code
),但CodeId
中的数据不能在AggregationChildren
表中。
我的问题是:它是否需要AggregationChildrenConfiguration
类中的以下配置?
HasRequired(ag_ch => ag_ch.Code)
.WithOptional(c => c.AggregationChild)
.WillCascadeOnDelete(false);