是否可以直接引用多对多表使用实体框架,首先是代码 [英] Is it possible to directly reference a many-to-many table using entity framework, code first
问题描述
我已经使用流畅的API创建了两个实体之间的关系(假设用户和组):
this.HasMany(t => t.Users)
.WithMany(t = > t.Groups)
.Map(
m =>
{
m.ToTable(GroupMembers);
m.MapLeftKey(Group_Id ;
m.MapRightKey(User_Id);
});
这很好,但我也想直接引用GroupMembers表。为了做到这一点,我有一些类似的东西:
[Table(GroupMembers)]
public class GroupMember
{
#region属性
///< summary>
///获取或设置组。
///< / summary>
public virtual Group Group {get;组; }
///< summary>
///获取或设置rht组的ID。
///< / summary>
[Key]
[Column(Group_Id,Order = 1)]
public int GroupId {get;组; }
///< summary>
///获取或设置用户。
///< / summary>
public virtual User User {get;组; }
///< summary>
///获取或设置用户的ID。
///< / summary>
[Key]
[Column(User_Id,Order = 2)]
public int UserId {get;组; }
#endregion
}
但是,我会得到在初始化DbContext时出现以下错误:
指定的模式无效。错误:(381,6):错误0019:已经定义了
具有模式'dbo'和表'GroupMembers'的EntitySet'GroupUser'为
。每个EntitySet必须引用唯一的模式和
表。
我相信这是因为实体框架没有意识到在流畅的API和GroupMembers实体表中指定的GroupMembers表实际上是一个。换句话说,如果我删除描述多对多关系的流畅的API代码,那么我可以成功地初始化DbContext。
我可以有一个多对多的表,我也可以直接引用?
不,你不能。如果您希望通过单独的实体访问连接表,则必须通过两个一对多关系来替换多对多关系,并更改用户$ c $中的导航属性c>和
组
来指代 GroupMember
:
public class Group
{
public int GroupId {get;组; }
public virtual ICollection< GroupMember>会员{get;组;
}
public class User
{
public int UserId {get;组; }
public virtual ICollection< GroupMember>会员{get;组;
}
modelBuilder.Entity< Group>()
.HasMany(g => g.Members)
.WithRequired(gm => gm。组);
modelBuilder.Entity< User>()
.HasMany(u => u.Members)
.WithRequired(gm => gm.User);
为什么要这个 GroupMember
实体?它不包含任何商业意义,只有参考和关键。通常,您可以通过编写LINQ查询并使用组
和用户
DbSets来获取和修改连接表的任何内容/实体及其导航属性。
I am using the entity framework and modelling a many-to-many relationship.
I have created the relationship between the two entities using the fluent API (let's say users and groups):
this.HasMany(t => t.Users)
.WithMany(t => t.Groups)
.Map(
m =>
{
m.ToTable("GroupMembers");
m.MapLeftKey("Group_Id");
m.MapRightKey("User_Id");
});
This works great, but I'd like to also be able to reference the GroupMembers table directly. To do that, I have something like:
[Table("GroupMembers")]
public class GroupMember
{
#region Properties
/// <summary>
/// Gets or sets the group.
/// </summary>
public virtual Group Group { get; set; }
/// <summary>
/// Gets or sets the Id of rht group.
/// </summary>
[Key]
[Column("Group_Id", Order = 1)]
public int GroupId { get; set; }
/// <summary>
/// Gets or sets the user.
/// </summary>
public virtual User User { get; set; }
/// <summary>
/// Gets or sets the Id of the user.
/// </summary>
[Key]
[Column("User_Id", Order = 2)]
public int UserId { get; set; }
#endregion
}
However, I will get the following error during initializing of the DbContext:
Schema specified is not valid. Errors: (381,6) : error 0019: The EntitySet 'GroupUser' with schema 'dbo' and table 'GroupMembers' was already defined. Each EntitySet must refer to a unique schema and table.
I believe this is because the entity framework does not realize that the GroupMembers table specified in the fluent API and the GroupMembers entities table are actually one in the same. In other words, if I remove the fluent API code which is describing the many-to-many relationship, then I am successfully able to initialize the DbContext.
Can I have a many-to-many table that I can also reference directly?
No, you can't. If you want to have access to the join table via a separate entity you must replace your many-to-many relationship by two one-to-many relationships and change the navigation properties in User
and Group
to refer to GroupMember
:
public class Group
{
public int GroupId { get; set; }
public virtual ICollection<GroupMember> Members { get; set; }
}
public class User
{
public int UserId { get; set; }
public virtual ICollection<GroupMember> Members { get; set; }
}
modelBuilder.Entity<Group>()
.HasMany(g => g.Members)
.WithRequired(gm => gm.Group);
modelBuilder.Entity<User>()
.HasMany(u => u.Members)
.WithRequired(gm => gm.User);
Why do you want this GroupMember
entity? It doesn't contain any business meaning and has only references and keys. Usually you can get and modify any content of the join table by writing LINQ queries and by using the Group
and User
DbSets/entities and their navigation properties.
这篇关于是否可以直接引用多对多表使用实体框架,首先是代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!