2

I have an existing Database I am trying to use Entity Framework Code First From Database to generate C# entities for. To do this I am doing Add -> New Item -> ADO.NET Entity Data Model -> Code First from database inside Visual Studio 2015. When I go though the Entity Data Model Wizard I see that every table is selected but when the import finishes entities for some of the tables where not created. I have tried this twice and double checked that the table with no entities are indeed selected for import. No errors are thrown during the import so I am not sure why some of the tables are missing there entitles. What might be going wrong and how do I get an entities for every single selected table?

SQL Create Table Code for one of the missing tables:

CREATE TABLE [dbo].[ProgramControl] (
    [CodeName]    VARCHAR (80) NULL,
    [CodeValue1]  VARCHAR (50) NULL,
    [CodeValue2]  VARCHAR (50) NULL,
    [CodeValue3]  VARCHAR (50) NULL,
    [CodeValue4]  VARCHAR (50) NULL,
    [Description] TEXT         NULL
);
Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123
  • What kind of tables are not being recreated? Linking tables (two columns of foreign keys used to represent many-to-many relationships)? Show us the table definitions for those tables that don't make it through the tool. I suspect they're simply not required and can be represented via collections in the entities at the other ends of the FK relationships. – spender Jan 04 '16 at 20:55
  • @spender added some code for you. They are not linking tables and they are required as I need to query data out of them. – Matthew Verstraete Jan 04 '16 at 20:59

2 Answers2

8

EF code-first requires the use of a primary key on every entity, so the tool is not able to map these tables.

It looks like you might be able to work around this with some trickery, but adding a PK to every table is almost certainly the best approach.

Community
  • 1
  • 1
spender
  • 117,338
  • 33
  • 229
  • 351
  • So what are my options? I can't modify the database to add a PK to the tables. I have never used anything but EF to connect to and query DBs in ASP.Net – Matthew Verstraete Jan 04 '16 at 21:01
4

If Table does not have at least one parameter not null ADO.NET will not generate the class. I don't know if it is a bug, just happened to me. It is not necessary to have a primary key but a not null.

David Soler
  • 192
  • 1
  • 10