Why does Database First EF skip mapping some tables?
Asked Answered
F

6

4

I'm using Entity Framework 4 and with a Database First binding, and EF is not generating the entities for a few of my tables. I'm not getting any errors, and no matter how many times I select the tables to generate from the "Update Model from Database" popup menu on the design surface, the same tables are still missing from the model.

I get no errors in the wizard. They just don't get generated. Any clues?

Forever answered 25/5, 2011 at 16:30 Comment(2)
Does those table have primary key in them?Janssen
@Akash, that might be the pattern. Looks like all the fields in those tables are nullable, so EF probably can't presume a PK value. If you want to convert your comment to an answer I'll accept it.Forever
F
9

EF requires a primary key on the table. EF will not map tables for which it can't find or derive a primary key. If all columns are nullable, it can't assume a primary key. If one or more columns are not nullable, EF will evidently derive a primary key for the table.

Forever answered 1/6, 2011 at 3:23 Comment(0)
J
3

EF will ignore table without primary keys.

Janssen answered 26/5, 2011 at 6:13 Comment(4)
Minor clarification - EF will not map tables for which it can't find or derive a primary key. If all columns are nullable, it can't assume a primary key.Forever
No EF has no problem with nullable columns, it only checks for primary key. And primary key can not be nullable, so condition for nullable is not considered.Janssen
Well, all I can say is try it. If ALL columns are nullable, the table will silently fail to map. If you have one or more columns are not nullable, it derive a primary key and map the table.Forever
Derive primary key from where?Janssen
H
0

Options I can think of:

  • Did you check the box next to those tables?

  • Did you previously add them, then delete their entities but keep the cache of the tables?

  • If so you can remove them from entity browser window and re-add them or manually add entities and define the table they map to in mappings window.

  • Perhaps tables were classified as relations instead of entities?

  • You can manually add the entities and choose the table they map to in mappings window.
Hove answered 25/5, 2011 at 19:15 Comment(2)
Thanks for your reply, Danny. As I indicate in my question, I have repeatedly tried to remap them via the wizard, and they never map. I also select all tables to bind in the model when originally creating the model, too. I can probably create manual mappings for them if I have to, but that doesn't answer the question of why they don't get mapped in the first place, and also doesn't bode well for updating the model from the DB in the future.Forever
Try adding a new entity from toolbox, not from wizard and map it.Hove
A
0

A possibility is when you're using tables with some different field types, as hierarchy in SQL Server.

Amatory answered 1/4, 2015 at 18:51 Comment(0)
A
0

Actually, in my case, it doesn't work because I was using a hierarchyid field as a primary key and EF doesn't work with this field type, so, it didn't import the table, because a valid PK is required.

Amatory answered 1/4, 2015 at 18:57 Comment(0)
T
0

Without Primary Key Tables where Skip Automatically on EF, OtherWise You Fix a Value as Not Null.

Tref answered 29/9, 2017 at 3:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.