我对映射以下关系感到困惑。
我有以下三个相关实体:User
, Group
, GroupUser
.
User
包含有关用户帐户的group
信息、有关组的信息,并GroupUser
包含有关用户在组中的成员资格的信息。
public class User
{
List<GroupUser> Groups { get; set; }
}
public class Group
{
List<GroupUser> Users { get; set; }
}
public class GroupUser
{
Group Group { get; set; }
User User { get; set; }
string Position { get; set; }
DateTime Joined { get; set; }
}
我在组中创建了一个带有以下内容的单面映射User
:
HasMany(x => x.Groups)
.WithMany()
.Map(m =>
{
m.ToTable("UserGroups");
m.MapLeftKey("UserId");
m.MapRightKey("GroupId");
});
但是,当我尝试再次映射它时Group
HasMany(x => x.Users)
.WithMany()
.Map(m =>
{
m.ToTable("UserGroups");
m.MapLeftKey("GroupId");
m.MapRightKey("UserId");
});
我收到以下错误:
Schema specified is not valid. Errors:
(274,6) : error 0019: The EntitySet 'UserUserGroup' with schema 'dbo' and table 'UserGroups' was already defined. Each EntitySet must refer to a unique schema and table.
我想重用两个实体之间的映射表,我该怎么做?