在我的数据库中,我有两个表Book
和User
,它们具有名为BookXUser
的M对M关系表。在关系表中,有两个字段,分别是Book
和User
的外键,以及另一个名为bookShelf
的字段。由于仅使用实体框架,因此在插入bookShelf
的同时无法找到插入M对M关系的方法,因此我决定为关系表建立模型,其定义如下这个:
modelBuilder.Entity<Book>()
.HasMany<UserData>(s => s.user)
.WithMany(c => c.book)
.Map(cs =>
{
cs.MapLeftKey("bookID");
cs.MapRightKey("userID");
cs.ToTable("BookXUser");
});
modelBuilder.Entity<BookXUser>()
.HasRequired<UserData>(s => s.user)
.WithMany(g => g.bookXuser)
.HasForeignKey<int>(s => s.userID);
modelBuilder.Entity<BookXUser>()
.HasRequired<Book>(s => s.book)
.WithMany(g => g.bookXuser)
.HasForeignKey<int>(s => s.bookID);
当我执行以下代码时,问题就出现了:
userDataRepo.FindBy(user => user.email == entry.email).FirstOrDefault()
The EntitySet 'BookUserData' with schema 'dbo' and table 'BookXUser' was already defined. Each EntitySet must refer to a unique schema and table.
如何在将关系表保留为引用BookXUser
的模型的同时解决该错误?我的想法是,我在BookXUser模型中发现的虚拟方法上使用了延迟加载,因此可以更轻松地获取书籍和用户,还可以手动将数据插入其中,以便填充书架字段。
public class BookXUser : IEntityBase
{
public int ID { get; set; }
public int bookID { get; set; }
public virtual Book book { get; set; }
public int userID { get; set; }
public virtual UserData user { get; set; }
public string bookShelf { get; set; }
}
Book
实体
public class Book : IEntityBase
{
public int ID { get; set; }
public string title { get; set; }
public string isbn { get; set; }
public int noPage { get; set; }
public string edition { get; set; }
public string bLanguage { get; set; }
public byte[] bookPic { get; set; }
public string publisherSite { get; set; }
public string bookFormat { get; set; }
public DateTime releaseDate { get; set; }
public DateTime initialReleaseDate { get; set; }
public string publisher { get; set; }
public string overview { get; set; }
public virtual ICollection<Author> author { get; set; }
public virtual ICollection<Genre> genre { get; set; }
public virtual ICollection<UserData> user { get; set; }
public virtual ICollection<Rating> rating { get; set; }
public virtual ICollection<Review> review { get; set; }
public virtual ICollection<BookXUser> bookXuser { get; set; }
public Book()
{
this.author = new HashSet<Author>();
this.genre = new HashSet<Genre>();
this.user = new HashSet<UserData>();
this.rating = new HashSet<Rating>();
this.review = new HashSet<Review>();
this.bookXuser = new HashSet<BookXUser>();
}
}
UserData
实体
public class UserData : IEntityBase
{
public int ID { get; set; }
public string username { get; set; }
public string userpass { get; set; }
public string email { get; set; }
public byte[] userPic { get; set; }
public string userOverview { get; set; }
public DateTime joinedDate { get; set; }
public virtual ICollection<Book> book { get; set; }
public virtual ICollection<Review> review { get; set; }
public virtual ICollection<Rating> rating { get; set; }
public virtual ICollection<UserData> user { get; set; }
public virtual ICollection<BookXUser> bookXuser { get; set; }
public UserData()
{
this.book = new HashSet<Book>();
this.review = new HashSet<Review>();
this.rating = new HashSet<Rating>();
this.user = new HashSet<UserData>();
this.bookXuser = new HashSet<BookXUser>();
}
}
答案 0 :(得分:1)
您以错误的方式定义了Book
和UserData
实体。正如您在问题中所说的那样,您正在尝试在Book
和UserData
之间定义多对多关系,因此您添加了一个连接实体BookXUser
,因为该实体拥有数据{{1 }}。该联接实体将按约定BookShelf
用作表名。
还定义了两个集合:
BookXUser
实体中的ICollection<UserData> user
Book
实体中的ICollection<Book> book
您还可以在UserData
方法中使用这种流畅的配置:
OnModelCreating
这样做,您将在modelBuilder.Entity<Book>()
.HasMany<UserData>(s => s.user)
.WithMany(c => c.book)
.Map(cs =>
{
cs.MapLeftKey("bookID");
cs.MapRightKey("userID");
cs.ToTable("BookXUser"); // <-- this is your error.
});
和Book
之间添加另一个多对多关系,并使用UserData
作为连接表的名称,该表已被您创建的实体BookXUser
。
要解决您的问题,您不需要像您一样流畅地配置集合和配置多对多关系。为什么?因为您有一个加入实体BookXUser
。
所以:
BookXUser
实体中删除ICollection<UserData> user
,并将其替换为Book
ICollection<BookXUser> BookXUser
实体中删除ICollection<Book> book
,并将其替换为UserData
ICollection<BookXUser> BookXUser
和UserData
之间的多对多关系而添加的流畅配置。