HasRequired和WithOptional E.F. 6.1.3需要吗?

时间:2017-09-27 07:28:23

标签: c# entity-framework

我有一个带有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);

0 个答案:

没有答案