11

I built a simple ADO.Net MVC project in Visual Studio 2015 by following the tutorial found here. My project works, but I wanted to add more tables to the database and create several foreign key relationships between them and the existing tables. I added an ADO.Net Entity Data Model (Project Properties > Add > New Item > Data > ADO.Net Entity Data Model > EF Designer from Data Model) with all tables selected. I'm using the (localdb)\MSSQLLocalDB that's installed with Visual Studio.

My intention was to create my new tables and FK-relationships in the .edmx diagram, then "Forward Engineer" the modified model back into the database. However, only 5 of the 6 tables created by the ASP.NET MVC template were added to the table. The AspNetUserRoles table was not added.

Can anyone explain why the table was not added and what I should have done to create the new tables and key relationships? I prefer to work in a graphical environment as I am not a DB/SQL expert.

d ei
  • 493
  • 4
  • 16
  • Did you happen to open any databases in Server Explorer in VS? – tshoemake Nov 11 '15 at 22:42
  • 2
    Probably a Table with User and Roles as Identies to form Keys this table wouldn't appear in the Diagram if so it will only appear in the Navigation Property. If the table is formed of two FK to form its primary key and no other properties it wont appear as Table in EF – Mark Homer Nov 12 '15 at 10:12
  • @Mark, It is a table of 2 Foreign Keys. Why does EF ignore tables like this? If I re-construct the DB from the model, will it restore this table? – d ei Nov 12 '15 at 13:12
  • @tshoemake, yes, I had the DB open in VS Server Explorer. Does that matter when using EF Designer? – d ei Nov 12 '15 at 14:16
  • @dei try closing the connection to the db in question in Sever Explorer, then try updating your model again. – tshoemake Nov 12 '15 at 14:24
  • @dei it doesn't ignore the table it is just not needed you just use the navigation property. something like (User Object).Roles.add(new userrole object) the intermediate join table is unrequired although it still exists under the hood – Mark Homer Nov 12 '15 at 14:44
  • Related post - [Table not mapped to Entity Framework Data Model](https://stackoverflow.com/q/10673044/465053) – RBT May 11 '18 at 06:58

4 Answers4

19

For anyone looking up this:

If you have a table formed of two Foreign Keys combined to form the Tables Primary Key and no other properties in the table the Entity Framework will not add this table to the Model although it exists in the background.

You do not need the intermediate Join table. The EF will add the Navigation property.

So in this case you would use something like: (User Object).Roles.add(new userrole object); save context changes to update;

Mark Homer
  • 960
  • 6
  • 15
0

You did not select AspNetUserRoles when you were initially generating the model. Do use the check boxes to select the tables you want.

To add it, Right click on the designer select "Update Model from Database..." from the context menu. Under the first Tab "Add". expand "Tables", then expand "dbo" and select "AspNetUserRoles" using the check box and other tables you may require. Click Finish button to update the Model and EDMX

Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
  • I thought maybe I'd missed selecting the table and tried this before posting the question to SO. It didn't pull in the table. – d ei Nov 12 '15 at 14:09
  • That is what I said. You did not select the table initially. But looks like instead of marking it as an answer you rather down voted my answer. Any reason for the down vote? – Julius Depulla Nov 12 '15 at 16:22
  • I didn't down-vote you. I don't have the rep to vote on comments - not that I would have if I did. I appreciate your taking time to answer, even if it wasn't the cause of my problem. It might help someone else. Mark Homer had the answer that applied to my case. Unfortunately, I couldn't up-vote him, either. – d ei Nov 12 '15 at 18:07
  • In my case i am not able to see the db table in the selection field. – Dileep Nov 27 '17 at 19:08
0

Ef requires primary key from table. If primary missing in table than EF will ignore that table .Find details here

Community
  • 1
  • 1
Krishna shidnekoppa
  • 1,011
  • 13
  • 20
  • Not true. If there is no Primary key EF designer will attempt to infer one (from all non-computed columns) if it cannot infer one you cannot add the table: all Entities must have a Primary Key – Mark Homer May 31 '18 at 11:59
0

You can add a primary key column in cross table . Then problem is resolving

Mehmet Kurt
  • 161
  • 2
  • 3