Bishnu Paudel
Bishnu Paudel

Reputation: 2079

Many to Many relation with junction table in Database first approach

I have three tables Author, Book and AuthorBook. AuthorBook table only contains two foreign keys pointing to Author and Book tables to achieve many to many relation between Author and Book table.

When I generated models from database, the Author and Book models are created without any association among them. I was expecting "Books" under Author model and "Authors" under Book model. Am I missing anything here or this is the way it works? Here is my table Diagram enter image description here

And here is my model diagram

enter image description here

Upvotes: 2

Views: 1380

Answers (2)

davee
davee

Reputation: 156

is the relation in ur database defined between Book BookAuthor and Author BookAuthor ? --> How do I create a foreign key in SQL Server?

Upvotes: 0

Bishnu Paudel
Bishnu Paudel

Reputation: 2079

The table BookAuthor was not included by Entity Framework because it did not have a primary key. I fixed this issue by defining a primary key (on authorid and bookid) on the table.

Upvotes: 3

Related Questions