DBContext多对多关系 - 数据库优先

时间:2022-10-05 15:13:52

What is the requirement in the database to make DBContext with database first consider two tables as many-to-many and dropping the linking table from the autocreated entities?

数据库中要求DBContext与数据库首先考虑两个表是多对多并从自动处理的实体中删除链接表的要求是什么?

I have in database

我有数据库

Table: Book

  • id (Integer, PK)
  • id(整数,PK)

  • name (String)

Table: Author

  • id (Integer, PK)
  • id(整数,PK)

  • name (String)

Table: BookAuthor

  • id (Integer, PK)
  • id(整数,PK)

  • book_id (Integer, FK)
  • book_id(整数,FK)

  • author_id (Integer, FK)
  • author_id(整数,FK)

And Entity Framework 6.0 keeps creating the BookAuthor as en entity instead of creating Author.Books and Book.Authors navigation properties.

Entity Framework 6.0不断创建BookAuthor作为实体,而不是创建Author.Books和Book.Authors导航属性。

1 个解决方案

#1


0  

Try removing the 'id' PK column from the BookAuthor table and just use a composite primary key of book_id and author_id then regenerate the DbContext. A primary key composed of the two columns (book_id, author_id) should be sufficient to uniquely identify entries into the linking, or associative, table.
I believe this will help Entity Framework understand how to model this relationship in the Database Context.

尝试从BookAuthor表中删除'id'PK列,只使用book_id和author_id的复合主键,然后重新生成DbContext。由两列(book_id,author_id)组成的主键应足以唯一地标识链接或关联表中的条目。我相信这将有助于实体框架了解如何在数据库上下文中建模这种关系。

#1


0  

Try removing the 'id' PK column from the BookAuthor table and just use a composite primary key of book_id and author_id then regenerate the DbContext. A primary key composed of the two columns (book_id, author_id) should be sufficient to uniquely identify entries into the linking, or associative, table.
I believe this will help Entity Framework understand how to model this relationship in the Database Context.

尝试从BookAuthor表中删除'id'PK列,只使用book_id和author_id的复合主键,然后重新生成DbContext。由两列(book_id,author_id)组成的主键应足以唯一地标识链接或关联表中的条目。我相信这将有助于实体框架了解如何在数据库上下文中建模这种关系。