C# EF 6.x:Can';具有多对多关系的t种子表
我目前正在试验EF,我有以下问题无法解决 我有多对多关系的用户和角色实体。当我试图用初始数据为数据库种子时,问题出现了。两个用户和两个角色(在下面的代码中)已成功播种。我可以在角色和用户表中看到条目。但是连接表只有一个条目具有C# EF 6.x:Can';具有多对多关系的t种子表,c#,.net,entity-framework,many-to-many,code-first,C#,.net,Entity Framework,Many To Many,Code First,我目前正在试验EF,我有以下问题无法解决 我有多对多关系的用户和角色实体。当我试图用初始数据为数据库种子时,问题出现了。两个用户和两个角色(在下面的代码中)已成功播种。我可以在角色和用户表中看到条目。但是连接表只有一个条目具有user1id和role1id。当我试图从db获取具有2个角色的用户时,它只有一个角色-role1。 我不知道为什么。我的错误在哪里?我如何才能正确地做到这一点?这是我的密码: 实体 public abstract class Entity { public int
user1id
和role1id
。当我试图从db获取具有2个角色的用户时,它只有一个角色-role1
。
我不知道为什么。我的错误在哪里?我如何才能正确地做到这一点?这是我的密码:
实体
public abstract class Entity
{
public int Id { get; set; }
}
用户
public class AppUser : Entity
{
...
public virtual ICollection<AppRole> Roles { get; set; }
public AppUser()
{
Roles = new SortedSet<AppRole>(new RoleComparer());
}
}
公共类AppUser:实体
{
...
公共虚拟ICollection角色{get;set;}
公共应用程序用户()
{
角色=新的分类集(新角色比较器());
}
}
角色
public class AppRole : Entity
{
public RoleEnum Role { get; set; }
public ICollection<AppUser> Users { get; set; }
public AppRole()
{
Users = new SortedSet<AppUser>(new UserComparer());
}
}
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);
}
}
公共类批准:实体
{
公共RoleEnum角色{get;set;}
公共ICollection用户{get;set;}
公众认可
{
Users=新的SortedSet(newusercomparer());
}
}
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
}
}
公共类用户映射:EntityTypeConfiguration
{
公共用户映射()
{
ToTable(“用户”);
...
#区域多对多
HasMany(usr=>usr.Roles)
.WithMany(r=>r.Users)
.Map(Map=>
{
map.ToTable(“用户和角色”);
map.MapLeftKey(“AppUserId”);
map.MapRightKey(“ApprovleId”);
});
#端区
}
}
种子代码
public class AppRole : Entity
{
public RoleEnum Role { get; set; }
public ICollection<AppUser> Users { get; set; }
public AppRole()
{
Users = new SortedSet<AppUser>(new UserComparer());
}
}
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);
}
}
公共类DropCreateTestDbAlways:DropCreateDatabaseAlways
{
受保护的覆盖无效种子(UnitTestContext上下文)
{
var role1=新批准();
var role2=newapprole(){Role=RoleEnum.Administrator};
context.Roles.Add(role1);
context.Roles.Add(role2);
var user1=new AppUser()
{
UserName=“RegularUser”,
电子邮件=”regular@email.com",
PasswordHash=“FGJSDBXNLSNLSDDSJSCLNCS”,
UserProfile=新的AppUserProfile()
};
var user2=新的AppUser()
{
UserName=“AdminUser”,
电子邮件=”admins@email.com",
PasswordHash=“FGJSDBXNLSNLSDDSJSCLNCS”,
UserProfile=新的AppUserProfile()
};
user1.Roles.Add(role1);
user2.Roles.Add(role1);
user2.Roles.Add(role2);
context.Users.Add(user1);
context.Users.Add(user2);
种子(上下文);
}
}
好的,我想我找到了我的问题。看来我的比较器的原因在哪里。 我用generic List替换了
SortedSet
,一切都按预期开始工作
以下是我的一个比较器的代码:
public class RoleComparer : IComparer<AppRole>
{
public int Compare(AppRole x, AppRole y)
{
return x.Id.CompareTo(y.Id);
}
}
公共类角色比较者:i比较者
{
公共整数比较(近似x,近似y)
{
返回x.Id.CompareTo(y.Id);
}
}
我仍然不太明白它为什么会引起我的问题,所以如果有人知道,请告诉我。好的,我想我已经找到了我的问题。看来我的比较器的原因在哪里。 我用generic List替换了
SortedSet
,一切都按预期开始工作
以下是我的一个比较器的代码:
public class RoleComparer : IComparer<AppRole>
{
public int Compare(AppRole x, AppRole y)
{
return x.Id.CompareTo(y.Id);
}
}
公共类角色比较者:i比较者
{
公共整数比较(近似x,近似y)
{
返回x.Id.CompareTo(y.Id);
}
}
我仍然不太明白为什么它会引起我的问题,所以如果有人知道,请告诉我