简体   繁体   English

实体框架不为表或过程生成类

[英]Entity Framework Not Generating Classes for Tables or Procedures

I'm using the Entity Framework to generate the classes and functions in C# I need to interact with the SQL server. 我正在使用Entity Framework在C#中生成类和函数,我需要与SQL服务器进行交互。

For reference, here's one of my tables: 作为参考,这是我的一个表:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Area](
    [ID] [bigint] identity (1, 1) primary key,
    [Name] [nvarchar](100) NOT NULL
    )
GO

After running the Entity Data Model Wizard (using 'EF Designer from database'), my project has the edmx file and a few new .cs files, but it seems like it's not generating everything it needs. 运行实体数据模型向导(使用'数据库中的EF Designer')后,我的项目有edmx文件和一些新的.cs文件,但似乎它没有生成它需要的所有内容。

In my DatabaseEntities class, for example, I've got: 例如,在我的DatabaseEntities类中,我得到了:

    public virtual DbSet<Area> Areas { get; set; }

However, there's no definition for the type 'Area' (along with three other missing types). 但是,“区域”类型(以及其他三种缺失类型)没有定义。 I'm also missing the functions for stored procedures. 我也缺少存储过程的功能。

I've tried deleting the new files and re-running the Model Wizard, but I get the same result. 我已经尝试删除新文件并重新运行模型向导,但我得到了相同的结果。

Has anyone else run into this? 有没有其他人遇到这个?

SIDENOTES: 图片的标题说明:

I've noticed during the last few attempts that I'm also getting an error when the wizard runs: "The Entity Framework package not installed on project". 我在最近的几次尝试中注意到,当向导运行时我也遇到错误:“未在项目上安装Entity Framework包”。 However, it's still generating the edmx and the model.context when I click past it. 但是,当我点击它时,它仍然生成edmx和model.context。

I've had the same problem with both Entity Framework versions 6.0.0 and 6.1.2. 我对Entity Framework版本6.0.0和6.1.2都有同样的问题。

Reinstalling the framework had no effect on the problem(s). 重新安装框架对问题没有影响。

UPDATE: 更新:

Uninstalling nuget and reinstalling the latest version allowed me to install EntityFramework via nuget without error. 卸载nuget并重新安装最新版本允许我通过nuget安装EntityFramework而不会出错。 However, now the ADO.NET data model is missing from the Add New Item dialogue. 但是,现在Add New Item对话框中缺少ADO.NET数据模型。

There were several steps involved in what I did, and I have to give some credit to the people who commented below the question. 我所做的工作涉及几个步骤,我必须对在问题下面评论过的人给予一定的评价。

1) I uninstalled nuget package manager and reinstalled the latest version (apparently mine was not fresh). 1)我卸载了nuget包管理器并重新安装了最新版本(显然我的并不新鲜)。 This allowed me to install EntityFramework via nugget with no errors or rollback messages. 这允许我通过nugget安装EntityFramework,没有错误或回滚消息。

2) I'm not sure if this helped or not, but I also reinstalled Entity Framework Tools for Visual Studio via Microsoft's website. 2)我不确定这是否有帮助,但我也通过微软的网站重新安装了Visual Studio的实体框架工具。 I'm still not sure if it's necessary to have both. 我仍然不确定是否有必要同时拥有这两者。

3) The ADO.NET Entity Data Model template appeared to be missing from the Add New Item dialogue. 3)Add New Item对话框中似乎缺少ADO.NET实体数据模型模板。 After selecting "Add -> Component" instead of "Add -> New Item", it then mystically appeared under both lists. 选择“添加 - >组件”而不是“添加 - >新项目”后,它会神秘地出现在两个列表下。

Once that was done, I was able to run EF Designer and everything generated with no problem. 完成后,我就可以运行EF Designer,并且生成的所有内容都没有问题。

Make sure the table has a key column. 确保该表具有键列。 It will not generate the view if there is no key column in a table. 如果表中没有键列,则不会生成视图。

Make sure that your column names are not reserved words such as datatypes or keywords; 确保列名不是数据类型或关键字等保留字; I had a problem today trying to import a table with a column that someone had simply called "DATE", which is obviously a datatype, and that goofed up EF. 我今天遇到了一个问题,试图导入一个列有人简称为“DATE”的表,这显然是一种数据类型,而且这种情况很糟糕。 When I created a view in SQL and aliased the column with an unreserved name, the table imported just fine and generated all the requisite classes. 当我在SQL中创建一个视图并使用无保留名称别名时,该表导入得很好并生成了所有必需的类。

Personally I would highly recommend going with a Code-First approach rather than the designer. 就个人而言,我强烈建议采用Code-First方法,而不是设计师。 As the designer is nothing but trouble!!! 因为设计师不过是麻烦!!!

Code First Tutorial Code First教程

Calling Stored Procedures from Code First 从Code First调用存储过程

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

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