GlobalConfiguration does not contain a definition

2020-07-03 07:49发布

问题:

I'm attempting to create an ASP.NET MVC site that uses Database First Entity Framework.

I'm using creating a new MVC 5 site in Visual Studio Professional 2013 Update 3, then adding an ADO.NET Entity Data Model from which I choose EF Designer from database.

After that, I create a connection string, select the tables to generate the framework, and all is well.

However when I compile it, I receive this error

'MyModel.GlobalConfiguration' does not contain a definition for 'Configure'

This is in Global.asax.cs and errors here

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

GlobalConfiguration is automatically generated with the added entity framework for MyModel. It certainly doesn't have a Configure method.

public partial class GlobalConfiguration
{
    public int ID { get; set; }
    public long CurrentUSN { get; set; }
    public string DBVersion { get; set; }
    public long CurrentFile { get; set; }
}

I followed all of the answers here GlobalConfiguration.Configure() not present after Web API 2 and .NET 4.5.1 migration but none worked. It's a slightly different situation as this isn't an upgrade but is instead a clean project.

All Extensions and NuGet packages are up to date. I've even deleted the generated entity and reinstalled all NuGet packages to see if that helped, but no luck.

Any suggestions?

回答1:

  1. Open the Package Manager Console for the Project with the error.
  2. Type in Install-Package Microsoft.AspNet.WebApi.WebHost and hit Enter.
  3. Rebuild your Project.


回答2:

It looks like there is some sort of namespace conflict with the generated code. I'm fairly weary about this change, but everything has worked thus far.

Apparently GlobalConfiguration refers to MyModel.GlobalConfiguration.

However it's really looking for System.Web.Http.GlobalConfiguration

I switched this and all is working. What a hassle

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}


回答3:

1 - Make sure the files in your Team Explorer, if perform checkin;

2 - Check for old files in "packages" concerning the dll WebAPI such as the folder "Microsoft.AspNet.WebApi.WebHost.5.2.3" folder, if there are clear;

3 - After leaving everything clean install the project;

Good luck!!!!!