2

When I create a model using the "Database First" approach in my ASP.NET MVC 5 project, not all my tables from SQL Server are created in my project.

I've followed everything in this article step by step: https://www.asp.net/mvc/overview/getting-started/database-first-development/creating-the-web-application

ASP.NET MVC Project Model:

enter image description here

SQL Server Database:

enter image description here

There are three tables from my SQL Server database that haven't been created in the model. The table are AspNetUserRoles, ContactsAddresses, and IssuesItems

Here are the designs for the three missing tables:

AspNetUserRoles:

enter image description here

ContactsAddresses:

enter image description here

IssuesItems:

enter image description here

All three of the missing tables were created in SQL Server to eliminate many to many relationships and they all have two primary keys that connect two tables.

Why is this happening and what can I do to fix it?

Thank you.

2 Answers 2

6

EF by convention avoid to map junction tables that only have PK/FKs columns.It will take care to handle (populate/consult/etc) those tables for you. If you check, for example,your Issue entity, you will see a collection property of type Item.

public virtual ICollection<Item> Items{get;set;}

The same happens to the Item entity, and the rest of your entities.

When you write a query where is involve one of these collection properties, EF will generate the proper sql statement for you involving the junction table, which helps you to cross that bridge without the necessity of write additional code.

That doesn't mean is not possible map those tables in EF, in fact there are people that recommend avoid this kind of relation map for more clarity and to be closer to what you normally do in SQL. But to do that you will need to jump to Code First. You can, for example, generate your model using a tool like EF Power Tools or, in fact, you can use the ADO.NET Entity Data Model but using Code First from database option instead of EF Designer from database. Using of those tools your model will be generated as you already have, but you can add classes to represent your junction tables and create one to many relationships between them and the entities involves like the example in this blog post, but IMHO I prefer EF takes care those tables for me.

Sign up to request clarification or add additional context in comments.

2 Comments

Agreed. Unless the "relationship has scalar attributes", you don't get an extra "object" (that represents a table)..you get child collections (as octaviocci describes). EF is working correctly. If you had an EmployeeToJobTitle (M:N) and that table had a "StartDate" (for the ~relationship).....you would get the "extra" entity in the .cs classes. Its a mindshift a little. It's not a one for one relationship.
the one reason I would not let EF handle the situation is if I thought there would be a possible attribute on the relationship in the future....that may take a mini crystal ball, but sometimes I have a gut-feeling that "it isn't always going to be a simple M:N". I always suggest people do a Proof of Concept.and have 2 M:N. The "simple" one and the complex one with attributes on the relationship. That way , you know how to code both options.
1

As when searching SO I came to this question and this answer may not completely related to the explained question but for those who is looking for the answer why not all the table is getting created is

You need to add primary key attribute for that column specially if you have relation with that table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.