Entity Framework: foreign keys for many-to-many relations

c# entity-framework-6

Question

Concerning the navigational characteristics of many-to-many relations, I have an issue. According to what I read on MSDN, there is aForeignKey an asset to a navigational asset. Now that I'm trying to utilise this feature for many-to-many relationships, I'm having trouble making it work.

Utilizing EF 6.1.3

my courses

public class Class1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    [ForeignKey(nameof(Class2s))]
    public ICollection<Guid> Class2Ids { get; set; }

    [ForeignKey(nameof(Class2Ids))]
    public ICollection<Class2> Class2s { get; set; }

    [ForeignKey(nameof(Class3))]
    public Guid Class3Id { get; set; }

    public Guid Class3 { get; set; }
}

public class Class2
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    [ForeignKey(nameof(Class1s))]
    public ICollection<Guid> Class1Ids { get; set; }

    [ForeignKey(nameof(Class1Ids))]
    public ICollection<Class1> Class1s { get; set; }
}

public class Class3
{ 
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; } 
}

The issue is that although the navigational property loads, the id collection does not.

public static void Main(string[] args)
{
    using (SomeContext context = new SomeContext())
    {
        var classes = context.Classe1Set
            .Include(c => c.Class2s)
            .Include(c => c.Class2s)
            .First();

        classes.Class3;     // filled with correct object
        classes.Class3Id;   // filled with correct guid

        classes.Class2s;    // is filled with 2 elements
        classes.Class2Ids;  // problem: is Empty List
    }
}

Why am I misusing this?

Edit: updated the classes to better depict the issue. On the DB side, the many-to-many connection is set up successfully. The only thing wrong with the property is that it doesn't fill up appropriately, but Class3Id does.

I appreciate any support.

1
2
3/21/2017 1:09:31 PM

Accepted Answer

ZZZ_tmp
0
3/21/2017 12:46:04 PM

Popular Answer

An example of a Many-to-Many connection between Class1 and Class2 is given below. Below, Class1 should have a collection navigation property for Class2, and Class2 should have a collection navigation property for Class1.

public class Class1
{
    public Class1() 
    {
        this.Courses = new HashSet<Course>();
    }

    public int Class1Id { get; set; }

    public virtual ICollection<Class2> Class2s { get; set; }
}

public class Class2
{
    public Class1()
    {
        this.Class1s = new HashSet<Class1>();
    }

    public int Class2Id { get; set; }

    public virtual ICollection<Class1> Class1s { get; set; }
}

When that happens, a table with the nameClass1Class2 Having a Class1 Class1Id field serving as the main key and another Class2 Class2Id serving in the same capacity.

By default, EF searches for foreign key properties with the same name as the primary key name of the main entity. If the foreign key property is missing, EF will add an FK column to the database table with the prefix "_," for example. If Class1 entity does not have a foreignkey attribute for Class1 where Class1 has Class1Id, EF will construct Class1 Class1Id foreign key column into Class1 table.



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