Entity Framework 6 Database First Approach Not Creating All Tables From SQL Server

asp.net-mvc asp.net-mvc-5 entity-framework entity-framework-6 sql-server


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:


enter image description here


enter image description here


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/29/2016 6:12:33 PM

Accepted Answer

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.

2/26/2016 10:06:41 PM

Popular Answer

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.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow