问题
I'm using the Entity Framework to generate the classes and functions in C# I need to interact with the SQL server.
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.
In my DatabaseEntities class, for example, I've got:
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". However, it's still generating the edmx and the model.context when I click past it.
I've had the same problem with both Entity Framework versions 6.0.0 and 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. However, now the ADO.NET data model is missing from the Add New Item dialogue.
回答1:
Make sure the table has a key column. It will not generate the view if there is no key column in a table.
回答2:
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). This allowed me to install EntityFramework via nugget with no errors or rollback messages.
2) I'm not sure if this helped or not, but I also reinstalled Entity Framework Tools for Visual Studio via Microsoft's website. 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. 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.
回答3:
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. 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.
回答4:
Personally I would highly recommend going with a Code-First approach rather than the designer. As the designer is nothing but trouble!!!
Code First Tutorial
Calling Stored Procedures from Code First
来源:https://stackoverflow.com/questions/28572125/entity-framework-not-generating-classes-for-tables-or-procedures