在 EF 中,没有代表网桥表的对象。但是我需要删除/添加记录到桥表,所以我想创建一个代表桥表的类
例如:
User: userID, name
Group: groupID, name
tblUserGroup: userID, groupID (bridge table)
我有以下内容:
public class EFDbContext : DbContext
{
public DbSet<User> User { get; set; }
public DbSet<Group> Group { get; set; }
public DbSet<GroupUser> tblGroupUser { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().ToTable("User");
modelBuilder.Entity<Group>().ToTable("Group");
modelBuilder.Entity<GroupUser>().HasKey(a => new { a.UserID, a.GroupID });
modelBuilder.Entity<Group>()
.HasMany(u => u.User)
.WithMany(g => g.Group)
.Map(m =>
{
m.MapLeftKey("GroupID");
m.MapRightKey("NetworkUserID");
m.ToTable("tblGroupUser");
});
}
}
当我想使用桥接表时
context.tblGroupUser...(do sth)
错误说
Invalid object name 'dbo.GroupUsers'.
我认为它说它不知道 GroupUser 映射到哪个表
但是当我添加
modelBuilder.Entity<GroupUser>().ToTable("tblGroupUser");
错误更改为
Each EntitySet must refer to a unique schema and table.
现在它不允许我映射 tblGroupUser 2 次。我该如何解决这个问题
其他信息
public class EFGroupkUser
{
private EFDbContext context = new EFDbContext();
public IQueryable<GroupUser> tblGroupUser
{
get { return context.tblGroupUser; }
}
}