Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing associations when building a model from existing SQL server database

Edit: the issue is solved in the newer versions of EF. At least that's what I assume since I recently created a new project and followed the same steps and everything went fine! Thanks for your comments and helps. I leave the original question, untouched, below.

I'm using the Entity Framework for the first time. I have a strange problem: I have this small database on SQL Server. Here is the diagram from the management studio:

enter image description here

As you can see, the foreign keys are all defined. To generate the edmx model, I selected "New Item -> ADO.NET Entity Data Model -> Generate from database". This is what I get:

enter image description here

All associations but two are missing! This is odd since the good old L2S could recognize them with no problem:

enter image description here

Can anybody help me fix it?

like image 330
AlefSin Avatar asked Nov 11 '11 15:11

AlefSin


People also ask

Which of the following is the correct command to create model based on your existing database in Entity Framework Core?

You use the DbContext Scaffold command to generate the model. The command has two required arguments - a connection string and a provider. The connection string will depend on your environment and database provider.

Which of the following approach can be used in EF core to create entity data model?

Now, to model your entities, there are three approaches in EF Core. These include: Code First, Model First and Database First. When following the Code First approach you would typically create the entity classes initially.


2 Answers

The answer to our problem in this case is that we had Unique Non-Clustered Index on some of the primary keys - this caused a problem for the EF model (but not in Linq2SQL). Changing the indexes on these keys to Unique Clustered Index fixed the problem!

like image 137
Stian Avatar answered Sep 28 '22 10:09

Stian


Are you checking the option of "Include foreign key columns in the model"?

EF Entity Data Model Wizard Screenshot
(source: msdn.com)

like image 45
amit_g Avatar answered Sep 28 '22 09:09

amit_g