This is probably just because my knowledge with the EF Code First fluent API is lacking, but I\'m stumped.
I want to model the following:
Based on Ladislav's excellent answer, here's how to do it without using any mappings - just attributes applied to the Model classes themselves:
public class Group
{
public int Id { get; set; }
[MaxLength(300)]
public string Name { get; set; }
public ICollection Users { get; set; }
}
public class User
{
public int Id { get; set; }
[MaxLength(300)]
public string Name { get; set; }
[ForeignKey("PrimaryGroup")]
public int PrimaryGroupId { get; set; }
[Required]
public Group PrimaryGroup { get; set; }
[InverseProperty("Users")]
public ICollection SecondaryGroups { get; set; }
}
If you want, you can add the virtual keyword to the 2 ICollection
s and the Group
. This allows lazy-loading. Performance-wise, I don't recommend it, but it is possible.
I included MaxLength
attributes with an arbitrary (but safe) length of 300
, because putting strings out in EF without a MaxLength gets you low-performance NVarChar(MAX)
columns. Totally irrelevant to what's being asked but better to post good code.
I recommend against class names "User" and "Group" for your EF classes. They're going to complicate any SQL you attempt to run later, having to type [User] and [Group] to access them, and complicate using these classes in MVC Controllers where your class User
will conflict with the Context
property User
that gives you access to the Asp.Net Identity library.