简体   繁体   English

EF Code First从DB不为所有表创建实体

[英]EF Code First From DB not creating Entities for all tables

I have an existing Database I am trying to use Entity Framework Code First From Database to generate C# entities for. 我有一个现有的数据库我试图使用Entity Framework Code First From Database来生成C#实体。 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. 为此,我正在Add -> New Item -> ADO.NET Entity Data Model -> Code First from database Visual Studio 2015中的Add -> New Item -> ADO.NET Entity Data Model -> Code First from database 。当我通过Entity Data Model Wizard我看到每个表都被选中但是当导入时完成某些未创建的表的实体。 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: SQL为缺少的表之一创建表代码:

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
);

EF code-first requires the use of a primary key on every entity, so the tool is not able to map these tables. EF代码优先需要在每个实体上使用主键,因此该工具无法映射这些表。

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. 看起来你可以通过一些技巧来解决这个问题 ,但是为每个表添加一个PK几乎肯定是最好的方法。

If Table does not have at least one parameter not null ADO.NET will not generate the class. 如果Table没有至少一个参数not null ADO.NET将不会生成该类。 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. 没有必要使用主键但不是空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM