I'm currently experimenting with EF and I have following problem which I can't solve.
I have User and Role entities with many-to-many relationship. The problem appears when I'm trying to seed database with initial data. Two users and two roles(in teh code below) are seeded successfully. I can see entries in Roles and Users tables. But junction table has only one entry with user1 id
and with role1 id
. When I'm trying to get user with 2 roles from db, It has only one role - role1
. And I don't know why. Where is my mistake and how can I do this correctly? Here's my code:
Entity
public abstract class Entity
{
public int Id { get; set; }
}
User
public class AppUser : Entity
{
...
public virtual ICollection<AppRole> Roles { get; set; }
public AppUser()
{
Roles = new SortedSet<AppRole>(new RoleComparer());
}
}
Role
public class AppRole : Entity
{
public RoleEnum Role { get; set; }
public ICollection<AppUser> Users { get; set; }
public AppRole()
{
Users = new SortedSet<AppUser>(new UserComparer());
}
}
FluentAPI
public class UserMap : EntityTypeConfiguration<AppUser>
{
public UserMap()
{
ToTable("Users");
...
#region Many-to-Many
HasMany(usr => usr.Roles)
.WithMany(r => r.Users)
.Map(map =>
{
map.ToTable("UsersAndRoles");
map.MapLeftKey("AppUserId");
map.MapRightKey("AppRoleId");
});
#endregion
}
}
Seed code
public class DropCreateTestDbAlways : DropCreateDatabaseAlways<UnitTestContext>
{
protected override void Seed(UnitTestContext context)
{
var role1 = new AppRole();
var role2 = new AppRole() { Role = RoleEnum.Administrator };
context.Roles.Add(role1);
context.Roles.Add(role2);
var user1 = new AppUser()
{
UserName = "RegularUser",
Email = "regular@email.com",
PasswordHash = "FGJSDBXNLSNLSDDSJSCLNCS",
UserProfile = new AppUserProfile()
};
var user2 = new AppUser()
{
UserName = "AdminUser",
Email = "admins@email.com",
PasswordHash = "FGJSDBXNLSNLSDDSJSCLNCS",
UserProfile = new AppUserProfile()
};
user1.Roles.Add(role1);
user2.Roles.Add(role1);
user2.Roles.Add(role2);
context.Users.Add(user1);
context.Users.Add(user2);
base.Seed(context);
}
}
Ok, I think I've found my problem. It seems that my Comparers where the cause. I replaced SortedSet<>
with generic List, and everything started working as expected.
Here is the code of one of my comparers:
public class RoleComparer : IComparer<AppRole>
{
public int Compare(AppRole x, AppRole y)
{
return x.Id.CompareTo(y.Id);
}
}
I still can't quite understand why it was causing my problems, so if someone knows, please, tell me.
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.