0

I extended my identity ApplicationUser profile (class) with list of values (Group model class):

 public class ApplicationUser : IdentityUser
 {
      public virtual ICollection<Group> group { get; set; }
    ...
 }

As my Group class is defined in another project in the solution, I cannot add a reference to the ApplicationUser(s) from Web project in that class (circular referencial integrity problem). So, when I run update-database ef code-first migrations created the join table Groups to connect the ApplicationUser and the Group, which have two columns, a key from Group class and a ApplicationUser's id key.

I have some problems here. First, generated Groups table have only Group id as it's primary key, not both ids as it should be. Second, when I try to remove group from a user in my application or to delete a user, ef try to update (set null) ApplicationUser's id column in join Groups table, which I don't want. It need to delete the row.

How can I fix this problem?

Milan M.
  • 959
  • 3
  • 12
  • 27

1 Answers1

0

You could try a fluent configuration that would tell EF how to handle the relationship, something like:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

   modelBuilder.Entity<ApplicationUser>()
               .HasMany<Group>(g => g.Groups)
               .WithMany(u => u.ApplicationUsers)
               .Map(ug =>
                        {
                            ug.MapLeftKey("UserId");
                            ug.MapRightKey("GroupId");
                            ug.ToTable("UserGroup");
                        });

}

See https://msdn.microsoft.com/en-us/data/jj591620.aspx

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • I tried this, but as I said, in Group class (which is in Data project) I cannot reference ApplicationUsers, as it is in Web project, because of circular dependency. – Milan M. Feb 27 '15 at 16:38
  • OK, I have my entities in a separate project as well, but I keep my context in the web project. – Steve Greene Feb 27 '15 at 21:11