c# - 实体框架:已定义具有架构的实体
问题描述
在我的数据库中,我有两个表,即Book
和User
,它们有一个名为 的 M 对 M 关系表BookXUser
。在关系表中,有两个字段,分别是 and 的外键Book
,User
另外一个字段名为bookShelf
。由于仅使用实体框架我无法找到插入 M 对 M 关系同时插入的方法bookShelf
,我决定为关系表创建一个模型,其定义如下:
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 模型中的虚拟方法上使用延迟加载,这样我可以更轻松地获取我的书籍和用户,我还手动将我的数据插入其中,以便我可以填充 bookshelf 字段。
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>();
}
}
解决方案
您以错误的方式定义了您Book
和UserData
实体。正如您在问题中所说,您正在尝试定义 and 之间的多对多关系Book
,UserData
因此您添加了一个 join 实体BookXUser
,因为该实体包含一个 data 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.
});
这样做,您将在 and 之间添加另一个多对多关系Book
,UserData
并用作您创建BookXUser
的实体已使用的连接表的名称。BookXUser
要解决您的问题,您不需要像以前那样流畅地添加集合和配置多对多关系。为什么?因为你有一个 join entity BookXUser
。
所以 :
ICollection<UserData> user
从您的实体中删除Book
并将其替换为ICollection<BookXUser> BookXUser
ICollection<Book> book
从您的实体中删除UserData
并将其替换为ICollection<BookXUser> BookXUser
UserData
删除您为和之间的多对多关系添加的流利配置Book
。
推荐阅读
- css - 在aspx页面中动态排列图像
- java - Guava 缓存数据加载问题
- war - ModuleNotFoundException:无法在 Jboss EAP 7 上部署战争
- batch-file - 有没有直接在浏览器的localhost上直接打开html文件?
- ag-grid - 是否可以在没有 ag-grid 企业版的情况下制作国家多选过滤器?
- php - PHP 致命错误:无法重新声明 Composer\Autoload\includeFile()
- reactjs - VS Code 自动导入不显示所有组件的建议
- python - wsgi.py 在 apache 上的 virtualenv 中失败,并显示“没有名为 'django' 的模块”
- c++ - 由于未定义的引用,无法链接到 Debian 上的 Crypto++
- python - pyspark 中的错误处理