I have a problem regarding navigational properties of many-to-many relations. As I read on MSDN, there is an option to define a ForeignKey
property to a navigational property. Now I want to use this feauture for many-to-many relation but I can't get it to work.
I am using EF 6.1.3.
My classes:
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 problem that the navigational property get loaded but the id collection stays empty.
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
}
}
What am I doing wrong?
Edit: Updated the classes to represent the problem better. The many-to-many connection is created correctly on the DB side. The problem is just the property Class1Ids not filling correctly whereas Class3Id does.
Thanks for any help.
You don't need the ForeignKey attributes - the relationship will be inferred from types.
Class1Ids
and Class2Ids
do not point to another entity, are not navigation properties, and cannot be loaded automatically by EF.
To get what you want you should define the Ids properties like so
public ICollection<Guid> ClassXIds { get { return ClassXs.Select(c => c.Id).ToList(); } }
public ICollection<Guid> ClassXIds { get { return ClassXs.Select(c => c.Id).ToList(); } }
. This doesn't load the data from DB, just pulls the Id's from the navigation property.
Below Class1 class should have a collection navigation property for Class2, and Class2 should have a collection navigation property for Class1, which will create a Many-to-Many relationship between Class1 and Class2 as shown below:
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; }
}
You will get then on your database a table named Class1Class2
with a field Class1_Class1Id as primary key and another Class2_Class2Id as primary key also
By default EF will look for foreign key property with the same name as principal entity primary key name. If foreign key property does not exists then EF will create FK column in Db table with + "_" + eg EF will create Class1_Class1Id foreign key column into Class1 table if Class1 entity does not contain foreignkey property for Class1 where Class1 contains Class1Id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.