首页 > 解决方案 > 实体框架:已定义具有架构的实体

问题描述

在我的数据库中,我有两个表,即BookUser,它们有一个名为 的 M 对 M 关系表BookXUser。在关系表中,有两个字段,分别是 and 的外键BookUser另外一个字段名为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>();
        }
    }

标签: c#entity-framework

解决方案


您以错误的方式定义了您BookUserData实体。正如您在问题中所说,您正在尝试定义 and 之间的多对多关系BookUserData因此您添加了一个 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 之间添加另一个多对多关系BookUserData并用作您创建BookXUser的实体已使用的连接表的名称。BookXUser

要解决您的问题,您不需要像以前那样流畅地添加集合和配置多对多关系。为什么?因为你有一个 join entity BookXUser

所以 :

  • ICollection<UserData> user从您的实体中删除Book并将其替换为ICollection<BookXUser> BookXUser
  • ICollection<Book> book从您的实体中删除UserData并将其替换为ICollection<BookXUser> BookXUser
  • UserData删除您为和之间的多对多关系添加的流利配置Book

推荐阅读