问题
I have a simple class called Applicant. I'm trying to add a template controller using the Entity Framework with Applicant as my model class, and a new data context.
Every time I try to create the controller, I get an error dialog that says "Unable to retrieve metadata for 'MyNameSpace.Models.Applicant'. There was an error generating 'ScaffoldingConnectionFactory'. Try rebuilding your project."
Rebuilding does nothing.
Here is my model class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace MyNameSpace.Models
{
public class Applicant
{
public int ApplicantId { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string MiddleInitial { get; set; }
[Required]
public string Phone { get; set; }
[Required]
public string EmailAddress { get; set; }
[Required]
[DataType(DataType.Date)]
public string DateOfBirth { get; set; }
public virtual Ethnicity Ethnicity { get; set; }
public virtual Race Race { get; set; }
}
public class Ethnicity
{
public int EthnicityId { get; set; }
public string Type { get; set; }
}
public class Race
{
public int RaceId { get; set; }
public string Type { get; set; }
}
}
I feel like I've missed a step but I can't put my finger on it.
回答1:
I had the same problem and had to fix it similarly.
My version of EF was incompatible with the new MVC tooling. After updating EntityFramework from NuGet, and making a few updates to outdated code, everything is working fine.
回答2:
This is a terrible answer and I'd like to understand the root cause, but my solution was to uninstall the EntityFramework library package reference from NuGet, then re-install it. Everything works now.
回答3:
I had the same issue. Tried re-installing EntityFramework using NuGet.
Didn't fix anything.
I'm about to bail and try using Linq.
I thought I had this working earlier!!
http://www.asp.net/mvc/tutorials/creating-model-classes-with-linq-to-sql-cs
Sometimes it seems like the MS stack is always changing so much it's unreliable. Is that why people use Linq?
EDIT: Running web platform installer on MS Visual web designer fixed this problem.
回答4:
I was just met this problem and then I realize that I have to choose the right one NuGet package to avoid this problem. There are two very familiar packages in the NuGet package list.
- The EFCodeFirst.SqlServerCompact is used with Entity Framework Feature CTP5.
- The EntityFramework.SqlServerCompact is used with the Entity Framework 4.1.
The "EntityFramework.SqlServerCompact" should be used if you are using Entity Framework 4.1 !!
回答5:
I got this problem when I tried to create a controller using the "Add Controller" dialog box, and by selecting the "Controller with read/write actions and views, using Entity framework" Template selection.
This generates code for a Controller class. By the way, this includes references to an EntityFramework Context object (such as, in my case for a PersonController, the following line: "Person person = db.People.Find(id);"). This is for a starting point only, and of course you can replace the auto-generated code, if you wish. Either way, this is why it asks for a Context class in the dialog, and why it will auto-gen a new one for you if requested.
I referenced Entity Framework using Nuget, which had the effect of referencing EntityFramework.dll v4.0. I later abstracted out the Data Access code into a different project, so then uninstalled EF through Nuget. There were some existing references to DbSet<> in the Context class, and I think I must have resolved it by getting Resharper to add a reference to get the project simply to get it to compile/build. R# added a reference to System.Data.Entity from the .Net 4 Framework library. It was at this stage that I started getting the error when trying to create new Controllers using the "Controller with read/write actions and views, using Entity framework" template.
I resolved it by (1) removing all references to System.Data.Entity; (2) installing Entity Framework through Nuget and rebuilding; (3) Creating the controller(s); (4) uninstalling EF through Nuget, removing the auto-generated Context class and replacing the autogenerated code in the controller with my own that referenced my classes from my DAL project/assembly.
回答6:
Had same problem. Problem is that the context is in separate assembly.
namespace WebJhs179.Models {
/// <summary>
/// This class is just wrapper to actual context. Because actual context is in separate assembly,
/// MVC3's controller creation fails.
/// Workaround: Create empty class and inherit actual context.
/// </summary>
public class Jhs179Context : Jhs179.Jhs179DbContext {
}
}
回答7:
I had the same problem while adding new controller in ASP.NET MVC 4, I solved the problem by moving the Database.SetInitializer(new SomeDataContextInitializer());
from the constructor of the DBContext to Application_Start method in Global.asax.cs.
Hope it helps.
回答8:
Can it be that the tools expect your Entity Framework EDMX to be in the same assembly as your website?
I've had the same error occuring in a project where the EDMX was in a separate assembly.
This was nessesary because we needed database access for a website as well as for a service.
回答9:
I had the same problem using EF 4.3.1. In my case it was because I had attached extra functionality to the generated DbContext using a partial class. When I removed this class from the model project, the scaffolding worked correctly.
回答10:
In my case the .edmx and model classes were in a separate assembly. This was not the issue, as I was able to get the scaffolding to work correctly simply be removing a partial class that I had used to extend the generated DbContext class.
回答11:
I was trying to use MVC 4 and Entity Framework 6. I finally figured out that MVC4 references the EF5 dll by default, uninstalled and installed EF6. They I got the message:
MVC Scaffolding does not support Entity Framework 6 or later. For more information please visit: [http://go.microsoft.com/fwlink/?LinkId=276833][1]
Solution was to use MVC 5 instead, as I had already written EF6 code, and the MVC app was new.
来源:https://stackoverflow.com/questions/5764778/error-generating-scaffoldingconnectionfactory