EF6 code first multiple 1-to-many mapping issue / "Multiplicity" error

c# database-design ef-code-first entity-framework entity-framework-6

Accepted Answer

I discovered the answer. Because I require TWO references from the,Interaction the class toElement class; it would be impossible to model this relationship in EF using just the attributes.

I had to update the model before instructing EF how to handle the relationships using the fluent API. I changed my model to look like this:

public class Element
{
    public Element()
    {
        CauseElements = new List<Interaction>();
        EffectElements = new List<Interaction>();
    }

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

    [Required, MaxLength(64)]
    public string Name { get; set; }

    #region Navigation

    public virtual ICollection<Interaction> CauseElements { get; set; }
    public virtual ICollection<Interaction> EffectElements { get; set; }

    #endregion
}

public class Interaction
{
    [Key]
    public int ID { get; set; }

    [Index]
    public int CauseID { get; set; }

    [Index]
    public int EffectID { get; set; }

    [MaxLength(64)]
    public string Location { get; set; }

    #region Navigation

    [ForeignKey("CauseID")]
    public virtual Element CauseElement { get; set; }

    [ForeignKey("EffectID")]
    public virtual Element EffectElement { get; set; }

    #endregion
}

And I utilized the Fluent API in my DbContext class to establish the connection between theInteraction.CauseElement and Element.CauseElements which asset served as the foreign key for theInteraction table (and for the Effect relationship as well)

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //Prevent cyclic cascade on elements table
    modelBuilder.Entity<Interaction>()
        .HasRequired(i => i.CauseElement)
        .WithRequiredDependent()
        .WillCascadeOnDelete(false);
    modelBuilder.Entity<Interaction>()
        .HasRequired(i => i.EffectElement)
        .WithRequiredDependent()
        .WillCascadeOnDelete(false);

    //Create the links between the element, the key, and the collection
    modelBuilder.Entity<Interaction>()
        .HasRequired<Element>(i => i.CauseElement)
        .WithMany(e => e.CauseElements)
        .HasForeignKey(i => i.CauseID);
    modelBuilder.Entity<Interaction>()
        .HasRequired<Element>(i => i.EffectElement)
        .WithMany(e => e.EffectElements)
        .HasForeignKey(i => i.EffectID);

    base.OnModelCreating(modelBuilder);
}

When you have a straightforward 1-to-many relationship, it appears that Entity Framework tries to deduce the relationships between the tables automatically. if I stoppedEffectElement using theInteraction course (andEffectElements from Element ), EF found it simple to establish the connection. However, when I re-added it, the issue reappeared.

given thatElement type was mentioned twice in theInteraction It had no idea how to establish the connection with the class. I had to define it specifically in theOnModelCreating method.

1
5/8/2015 3:45:02 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow