0

I have an ASP.NET identity user class that I wish to extend with a many-to-many relationship:

public class MyUser : IdentityUser
{
    public virtual ICollection<Group> Groups { get; set; }

    public MyUser()
    {
        Groups = new List<Group>();
    }
}

Group is defined thus:

public class Group
{
    public string Name { get; set; }
    //public ICollection<MyUser> MyUsers { get; set; }
}

When I add a migration for these objects, I get a MyUser_Id field on my Groups table, which gets overwritten whenever any user adds a group to their collection.

If I uncomment the other end of the relationship in the Group class, I do get a GroupMyUsers join table, which I can seed using Group. However, when I attempt to access the MyUser.Groups property an EntityCommandExecutionException is thrown, stating that 'MyUserGroups' is not a valid object.

pixelbadger
  • 1,556
  • 9
  • 24
  • What does your FluentAPI configuration look like? If the answer is, "I don't know", then take a look at https://msdn.microsoft.com/en-us/data/hh134698.aspx. You likely need to configure the relationship in the OnModelCreating method. – Wyatt Earp Jul 02 '15 at 16:33
  • I was hoping that the code-first conventions would take care of that side of things - am I expecting too much of it? – pixelbadger Jul 02 '15 at 16:35
  • Yes, if you want the junction table to be 'invisible'. FluentAPI is intended to be a part of code-first, not an exception to it. You can create your own junction table and do it with DataAnnotations if you like: http://stackoverflow.com/questions/18648600/many-to-many-mapping-using-data-annotations However, the way you're expecting it to work will, I believe, require one of those two solutions. – Wyatt Earp Jul 02 '15 at 16:40

0 Answers0