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?

John Kaster
  • 2,509
  • 1
  • 33
  • 40
  • 1
    Does those table have primary key in them? – Akash Kava May 25 '11 at 17:29
  • @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. – John Kaster May 26 '11 at 04:37

6 Answers6

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.

John Kaster
  • 2,509
  • 1
  • 33
  • 40
3

EF will ignore table without primary keys.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • 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. – John Kaster May 27 '11 at 00:28
  • 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. – Akash Kava May 27 '11 at 04:26
  • 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. – John Kaster Jun 01 '11 at 03:22
  • Derive primary key from where? – Akash Kava Jun 01 '11 at 03:49
0

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

Michel Ank
  • 11
  • 3
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.

Michel Ank
  • 11
  • 3
0

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

Abdulla Sirajudeen
  • 1,269
  • 1
  • 16
  • 29
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.
Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • 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. – John Kaster May 26 '11 at 04:29
  • Try adding a new entity from toolbox, not from wizard and map it. – Danny Varod May 26 '11 at 07:06