'Enable-Migrations' fails after upgrading

2019-01-27 13:52发布

问题:

I just upgraded my MVC4 project to .NET 4.5 and EF5 and started using VS2012. After realizing I needed to set-up auto-migrations in the Package Manager again I ran Enable-Migrations - EnableAutomaticMigrations and received the error

No context type was found in the assembly 'MySolutionName'.

Some Research has said that it has to do with EF5 not enabling prereleases. I ran Install-Package EntityFramework -IncludePrerelease but it said EF5 was already installed (which it was when I installed it through the NuGetmanager earlier without specifying -IncludePrerelease.

Does anyone know what I have to do to enable migrations for my project?

回答1:

I just had the same problem and found your question while searching for a solution.

I got it working. The problem, for me, was that I initially targeted the .NET 4.0 framework when I added the EF 5 via NuGet. Changing the target framework and then reinstalling EF 5 via NuGet, fixed it. It's also possible (see comments) that just reinstalling EF 5 via NuGet is the solution for you.

I had the following line in the App.config file, notice Version=4.4.0.0:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
</configuration>

So what I did was set the target framework to 4.5 inside the solution configuration and set the supported runtime to 4.5 too (inside the app config).

Old:

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

New:

  <startup>
    <supportedRuntime version="v4.5" sku=".NETFramework,Version=v4.5" />
  </startup>

After that change, I removed EF 5.0 via NuGet and added it again. It gave me the following configSection as result, notice Version=5.0.0.0:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
</configuration>

After that change, it worked.



回答2:

I was also faced the same problem, but after a day I suddenly get attention to a file called packages.config in the ASP MVC 4 project folder. So, here i get

 <package id="EntityFramework" version="6.0.2" targetFramework="net45" />

I changed the version number to my appropriate number i.e. for me i used EF ver 5.0.0. After I change the version, everything is fine now.