简体   繁体   English

首先在同一张表中启用Entity Framework 5代码中的多对多

[英]Enable multiple many to many in Entity Framework 5 code first to same table

I am new to Entity Framework. 我是实体框架的新手。 I am using Version 5 code first. 我首先使用版本5代码。

I have a model: 我有一个模型:

class RebateBase
{
    public int ID { get; set; }

    public RebateType RebateType { get; set; }

    public virtual ICollection<Customer> Customers { get; set; }

    public virtual ICollection<TurnoverClassification> AvailableTurnoverClassifications { get; set; }

    public virtual ICollection<TurnoverClassification> EnabledTurnoverClassifications { get; set; }


    public RebateBase()
    {
        if (AvailableTurnoverClassifications == null)
        {
            AvailableTurnoverClassifications = new List<TurnoverClassification>();
        }

        if (EnabledTurnoverClassifications == null)
        {
            EnabledTurnoverClassifications = new List<TurnoverClassification>();
        }

        if (Customers == null)
        {
            Customers = new List<Customer>();
        }
    }

and a model 和一个模型

class TurnoverClassification
{
    public string Name { get; set; }

    public virtual ICollection<RebateBase> RebateBases{ get; set; }

    [Key]
    public int NumericalValue { get; set; }
}

I already tried to initialize data with and without 我已经尝试使用和不使用初始化数据

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();


        modelBuilder.Entity<RebateBase>()
            .HasMany<TurnoverClassification>(rb => rb.AvailableTurnoverClassifications)
            .WithMany(tc => tc.RebateBases)
            .Map(rt =>
                {
                    rt.MapLeftKey("RebateID");
                    rt.MapRightKey("TurnoverID");
                    rt.ToTable("RebateBase_AvailableTurnoverClassifications");
                });

        modelBuilder.Entity<RebateBase>()
            .HasMany<TurnoverClassification>(rb => rb.EnabledTurnoverClassifications)
            .WithMany(tc => tc.RebateBases)
            .Map(rt =>
            {
                rt.MapLeftKey("RebateID");
                rt.MapRightKey("TurnoverID");
                rt.ToTable("RebateBase_EnabledTurnoverClassifications");
            });

But it doesn't work. 但这是行不通的。 With the upper approach I get the error: 使用上层方法,我得到了错误:

Schema specified is not valid. 指定的架构无效。 Errors: (9,6) : error 0040: Type RebateBase_AvailableTurnoverClassifications is not defined in namespace CustomerConfiguration.Datalayer (Alias=Self). 错误:(9,6):错误0040:在名称空间CustomerConfiguration.Datalayer(Alias = Self)中未定义类型RebateBase_AvailableTurnoverClassifications。

When I remove the second navigation property enabledturnoverClassifications everythings is fine and I don't need to adapt the mapping in modelbuilder. 当我删除第二个导航属性enabledturnoverClassifications时,一切都很好,不需要在modelbuilder中调整映射。 As soon as I add the second navigationproperty I get the error. 添加第二个导航属性后,我立即收到错误消息。

Could anybody please help? 有人可以帮忙吗?

Thank you. 谢谢。

Greetz 格蕾兹

Harry 哈利

You can use the InverseProperty attribute to specify the corresponding collection in the TurnoverClassification class to enable multiple many to many relationships. 您可以使用InverseProperty属性在TurnoverClassification类中指定相应的集合,以启用多个多对多关系。 The answer to this contains an example of multiple many to many relationships in EF. 这个问题的答案这个包含多个许多人在EF许多关系的一个例子。

I tested this with a Enable-Migrations specified and the correct relation tables were created. 我使用指定的Enable-Migrations进行了测试,并创建了正确的关系表。

class RebateBase
{
    public int ID { get; set; }

    public RebateType RebateType { get; set; }

    public virtual ICollection<Customer> Customers { get; set; }

    [InverseProperty("RebateBasesAvailable")]
    public virtual ICollection<TurnoverClassification> AvailableTurnoverClassifications { get; set; }

    [InverseProperty("RebateBasesEnabled")]
    public virtual ICollection<TurnoverClassification> EnabledTurnoverClassifications { get; set; }


    public RebateBase()
    {
        if (AvailableTurnoverClassifications == null)
        {
            AvailableTurnoverClassifications = new List<TurnoverClassification>();
        }

        if (EnabledTurnoverClassifications == null)
        {
            EnabledTurnoverClassifications = new List<TurnoverClassification>();
        }

        if (Customers == null)
        {
            Customers = new List<Customer>();
        }
    }

The model 该模型

class TurnoverClassification
{
    public string Name { get; set; }

    public virtual ICollection<RebateBase> RebateBasesAvailable{ get; set; }
    public virtual ICollection<RebateBase> RebateBasesEnabled{ get; set; }

    [Key]
    public int NumericalValue { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM