EF Code First - Add-Migration fails with Could not load file or assembly error
Hello,
I just hooked up a new project to use EF Migrations features and am encountering errors trying to add new migrations. I was successful at enabling migrations and getting InitialCreate script created, but when I try to call Add-Migration I get an error that it could not load the assembly for my project (or one of it's dependencies). Below is the full error output.
PM> Add-Migration AddNewParameters
System.IO.FileNotFoundException: Could not load file or assembly 'MyProjectName' or one of its dependencies. The system cannot find the file specified.
File name: 'MyProjectName'
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
Does anyone know what might cause this or have any advice on how to troubleshoot it? I'm stuck and any help would be much appeciated! Thank you.
Edit: I've also checked my configuration manager to ensure my assembly is being built in a possible configurations. Also, the same error occurs when running Update-Database.
Answer
I came across a similar problem recently.
I was using a DbContext class in a class library project in a solution where the library was referenced by a WCF service. The WCF service in turn was used by a website that was also in the solution but the website had no references to the class library. The startup project was set to the website and I got the same file not found error you are seeing.
After a bit of playing about, I eventually discovered that by making the WCF service the startup project the migration commands worked as expected. I also confirmed that the app.config in the startup project was used to resolve connection strings.
I finally concluded that to make it work it was necessary to set the startup project to any project that had a direct or indirect reference to the class library, including making the class library itself the startup project.
The migration tools seem to use the startup project as the target environment rather than using the project specified in the PMC, so the startup project has to end up with the class library containing the DbContext class in its bin directory and have the relavent app.config settings.
Hopefully this will help you resolve your problem.
Regards