与复合外键的1..n关系出错

时间:2017-05-16 11:13:14

标签: c# entity-framework

我正在使用C#,.NET Framework 4.0和Entity Framework Code First 6.1.3开发库。

当我尝试使用EF时出现此错误:

  

VariableData_AggregationLevelConfiguration_Source ::多重性是   在Role' VariableData_AggregationLevelConfiguration_Source'中无效   in relationship' VariableData_AggregationLevelConfiguration'。因为   依赖角色是指关键属性,即上界   依赖角色的多样性必须是' 1'。

我想设计一下:

ProductionOrder可能有一个或多个AggregationLevelConfiguration AggregationLevelConfiguration可以有零个或多个VariableData ProductionOrder可以包含一个或多个VariableData

这些是类:

public class AggregationLevelConfiguration
{
    public byte AggregationLevelConfigurationId { get; set; }
    public int ProductionOrderId { get; set; }

    [ ... ]

    public virtual ProductionOrder ProductionOrder { get; set; }
    public virtual IList<VariableData> VariableData { get; set; }
}

public class VariableData
{
    public string VariableDataId { get; set; }
    public int ProductionOrderId { get; set; }
    public byte AggregationLevelConfigurationId { get; set; }

    [ ... ]

    public virtual AggregationLevelConfiguration AggregationLevelConfiguration { get; set; }
}

public class ProductionOrder
{
    public int ProductionOrderId { get; set; }

    [ ... ]

    public ICollection<AggregationLevelConfiguration> AggregationLevelConfigurations { get; set; }

    public virtual ICollection<VariableData> VariableData { get; set; }
}

这些是配置类:

public class AggregationLevelConfigurationConfiguration : EntityTypeConfiguration<AggregationLevelConfiguration>
{
    public AggregationLevelConfigurationConfiguration()
    {
        HasKey(agl => new { agl.AggregationLevelConfigurationId, agl.ProductionOrderId });

        [ ... ]

        HasRequired(agl => agl.ProductionOrder)
            .WithMany(po => po.AggregationLevelConfigurations);
    }
}

public class VariableDataConfiguration : EntityTypeConfiguration<VariableData>
{
    public VariableDataConfiguration()
    {
        HasKey(vd => new { vd.AggregationLevelConfigurationId, vd.ProductionOrderId });

        [ ... ]

        HasRequired(vd => vd.AggregationLevelConfiguration)
            .WithMany(agl => agl.VariableData)
            .HasForeignKey(vd => new { vd.AggregationLevelConfigurationId, vd.ProductionOrderId });
    }
}

public class ProductionOrderConfiguration : EntityTypeConfiguration<ProductionOrder>
{
    public ProductionOrderConfiguration()
    {
        HasKey(po => po.ProductionOrderId);

        Property(po => po.ProductionOrderId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        [ ... ]

        HasRequired(po => po.Product)
            .WithMany(p => p.ProductionOrders)
            .HasForeignKey(po => po.ProductId);
    }
}

但我认为这是正确的。

有什么不对?或者,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您不能使用这样的主键建立一对多的关系:

 HasRequired(vd => vd.AggregationLevelConfiguration)
            .WithMany(agl => agl.VariableData)
            .HasForeignKey(vd => new { vd.AggregationLevelConfigurationId, vd.ProductionOrderId });

由于AggregationLevelConfigurationId, ProductionOrderIdAggregationLevelConfiguration中的主键,因此您只能拥有一对一的关系(根据EF)。你需要做的是这样的事情:

 HasRequired(vd => vd.AggregationLevelConfiguration)
            .WithMany(agl => agl.VariableData)
            .HasForeignKey(vd => new { vd.ForeignKeyInVariableData });

ForeignKeyInVariableData不是AggregationLevelConfiguration中的主键(或者甚至可能是主键的一部分)。