Entity Framework Code First Migrations - Enable-Mi

2020-02-10 09:12发布

问题:

I have been trying to use code-first migrations, and had some limited success, but one of the things I got wrong was that I failed to tell it which project had the config information. It generated some classes for me, and I sort-of got it all working, but I figured that I should sort out the configuration issue and start over.

Bad mistake. Now that I've attempted to remove all the migrations stuff, I find I cannot re-install it. I've un-installed and re-installed EF 5, but when I use the Enable-Migrations command, I get an exception report:

System.Runtime.Serialization.SerializationException: Type is not resolved for member 'System.Data.Entity.Migrations.Design.ToolingFacade+GetContextTypeRunner,EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

No matter what I do now, I can't seem to get past that. Any ideas?

回答1:

This happened to me because I had a special character in the path of my project - specifically, an ampersand &. I removed the ampersand and then everything worked like a charm.



回答2:

Well, I don't know why this wasn't a problem from the get-go, but it turns out that the issue was caused by my having some projects targetting .NET 4.5 and some targetting .NET 4.0. This means that those projects use different versions of EF 5 (v5 versus v4.4), and I think that was causing the problem; it was attempting to load the wrong version of the DLL.

When I unloaded the projects targetting .NET 4.5 from my solution, I could use Enable-Migrations on the projects targetting .NET 4.



回答3:

I have found another way this problem can occur so I would like to share it here for future reference :-) .

In our case we got an invalid binding redirect in our Web.config file:

<dependentAssembly>
   <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.2.0.0" />
   <codeBase version="6.0.0.0" href="libs/EntityFramework.dll" />
</dependentAssembly>

Even though the NuGet package version is 6.2 the assembly version is still 6.0.0.0. Using this redirect will probably not cause issues with your code, but will break the powershell EF commands as well as the migrate.exe tool.

The solution is either to remove the binding or using the proper version - 6.0.0.0.

<dependentAssembly>
   <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
   <codeBase version="6.0.0.0" href="libs/EntityFramework.dll" />
</dependentAssembly>


回答4:

Writing this cause maybe someday it will help someone - just had the same problem and the (stupid) issue was bad hostname at the connectionString.



回答5:

My project was receiving this error of naming convention. MyProject.MyProject.Web, MyProject.MyProject.EntityFramework, etc.

I changed it to MyProject.MyProject2.Web, etc.



回答6:

My Project folder path had a word Q&A. As soon as I changed to QA and re-run the migrations, this issue has gone away. Avoid '&' character in name of any folders in the project path.



回答7:

I had the same problem, which may have been caused by changing the name of the project and/or moving it in my project folder structure. It was, anyhow, solved by recreating the project and importing (add existing item) all my .cs files manually, then adding new migrations.