12

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.

Nightmare Games
  • 2,205
  • 6
  • 28
  • 46
  • If you are doing CodeFirst from an existing database you won't get arbitrary stored procs or tvfs since they are not natively supported by CodeFirst. – Pawel Feb 17 '15 at 22:16
  • For stored proc you might need to add next two lines: SET FMTONLY OFF SET NOCOUNT ON In the dialog you can create the complex types. It is recommended to remove FMTONLY ON again. For the tables, can you show the table definition ? – Ako Feb 17 '15 at 22:24
  • @Pawel: I'm using EF Designer from database. – Nightmare Games Feb 17 '15 at 23:23
  • Does the actual model look OK? Is the Area entity in there? – dudeNumber4 Feb 17 '15 at 23:35
  • @dudeNumber4: In the edmx model, I see all four of my tables on the diagram. – Nightmare Games Feb 17 '15 at 23:41
  • If you right click on the edmx in the solution explorer, you should see an option "run custom tool." When you click that, do you get errors? – dudeNumber4 Feb 17 '15 at 23:45
  • Does the project actually build or does it complain that these classes are not present? – Stephen Byrne Feb 17 '15 at 23:55
  • In VS, in the ouput window select edmx output from the dropdown and see if you have any errors... – Pawel Feb 17 '15 at 23:58
  • @StephenByrne: No, I'm getting "type or namespace not found" errors. – Nightmare Games Feb 18 '15 at 00:02
  • Anything in the "Output" window in Visual Studio after you run the Wizard? Also, what version of EF are you using? – Stephen Byrne Feb 18 '15 at 00:02
  • @Pawel: I don't have "edmx output", but I do have "Entity Data Model". That output shows several instances of "The model was generated with warnings or errors" but nothing specific. – Nightmare Games Feb 18 '15 at 00:04
  • @StephenByrne: I don't see anything in the ouput window aside from the errors I mentioned prior. It looks like I've got version 6.0.0.0. – Nightmare Games Feb 18 '15 at 00:06
  • Try opening the edmx file with an XML editor, it should contain these warnings or errors (right-click, "Open With..." and search for "warning" and "error"...If nothing in there, maybe remove the EF package from the project/solution and reinstall it :( – Stephen Byrne Feb 18 '15 at 00:11
  • @Cricketheads - this is what I was referring to just didn't remember the name from the top of my head. Do what Stephen Byrne recommends (i.e. open as xml) to get more details. – Pawel Feb 18 '15 at 03:16
  • @StephenByrne: There were no errors or abnormalities in the edmx as xml. I did actually see all my tables and procedures defined there, however. I've reinstalled the Entity Framework, this time as version 6.1.2. However, I'm still getting "The Entity Framework not installed on project x" from the wizard, and still getting the missing data type errors from the compiler. – Nightmare Games Feb 18 '15 at 18:52
  • @Cricketheads: Now that sounds like a nuget error. Are you "installing" EF via nuget? – dudeNumber4 Feb 18 '15 at 18:58
  • @dudeNumber4: I downloaded via the Microsoft Download Center: https://www.microsoft.com/en-us/download/details.aspx?id=40762 – Nightmare Games Feb 18 '15 at 19:10
  • @Cricketheads: I would install via nuget. Using UI, you can right click on your project, manage nuget packages, search online for Entity Framework, select version 6.1.2 – dudeNumber4 Feb 18 '15 at 19:15
  • @dudeNumber4: Attempting to install from nuget yields the messages "Installed failed. Rolling back... The EntityFramework package is not installed on project x." – Nightmare Games Feb 18 '15 at 20:17
  • @Cricketheads: Great; you're in nuget hell. Try running "install-package EntityFramework -Version:6.1.2" in the Package Manager Console window (ensuring the proper project is selected) just to see what errors it will show... – dudeNumber4 Feb 18 '15 at 21:32
  • @dudeNumber4: From the package manager console: Install failed. Rolling back... install-package : The EntityFramework package is not installed on project 'x'. At line:1 char:1 + install-package EntityFramework -Version:6.1.2 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Install-Package], RuntimeException + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand – Nightmare Games Feb 18 '15 at 21:50
  • @dudeNumber4: I just uninstalled nuget (which was not the newest version) and reinstalled the newest. It installed the EntityFramework with no errors this time. – Nightmare Games Feb 18 '15 at 21:59
  • The option to add a class of type ADO.NET is now missing from the Add New Item window. – Nightmare Games Feb 18 '15 at 22:01

4 Answers4

7

Make sure the table has a key column. It will not generate the view if there is no key column in a table.

3

I am facing this also, not generating the class files, discovered now that it is a bug of visual studio. I got the same and exact project with another older version of visual studio and worked, it generated all. Delete your visual studio and reinstall can solve or use a previous version.At my case 17.6.2 is not working and 17.3.5 is working(visual studio 2022)

Vinicius Sin
  • 1,571
  • 11
  • 8
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.

Nightmare Games
  • 2,205
  • 6
  • 28
  • 46
0

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.

Tim
  • 169
  • 9