none
EF Code First - Add-Migration fails with Could not load file or assembly error. RRS feed

  • Question

  • 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.


    • Edited by two_o_six Tuesday, April 24, 2012 6:31 PM Added More info
    Monday, April 23, 2012 11:13 PM

Answers

  • 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

    • Marked as answer by two_o_six Monday, April 30, 2012 8:18 PM
    Friday, April 27, 2012 1:34 PM
  • I encountered the same problem. You just have to reference in your start up project the project that contains the DbContext class. Hope this helps
    • Marked as answer by two_o_six Monday, April 30, 2012 8:07 PM
    • Unmarked as answer by two_o_six Monday, April 30, 2012 8:08 PM
    • Marked as answer by two_o_six Monday, April 30, 2012 8:18 PM
    Saturday, April 28, 2012 7:13 AM

All replies

  • Hi paul206,

    Welcome to MSDN Forum.

    Could you please clarify what's the 'MyProjectName' file is?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us


    Wednesday, April 25, 2012 3:26 AM
    Moderator
  • MyProjectName is the name of the assembly that includes my DbContext and that migrations is enabled for. It is actually a different name, but for IP reasons I cannot post that online. Thanks!
    Wednesday, April 25, 2012 6:07 PM
  • Hi paul206,

    I'm afraind I can't repro the issue, but from the error exception, have you load this file into your project?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 26, 2012 2:57 AM
    Moderator
  • Thanks for trying, Allen. The assembly that is referenced in the error exception is the project itself. That is why this error is so strange.

    So, to further the example, I have a project called MyProject that contains a DbContext class for creating my database using Code First. Using the PMC (Package Manager Console), I run Enable-Migrations with MyProject selected in the Default Project Dropdown. This works just fine.

    Next, I go add a new entity to my entities project (a separate project in my solution) and add a DbSet for that new entity to my DbContext in MyProject. Then, I run Add-Migration AddNewEntity in PMC and I get the exception noted indicating it cannot find the assembly that I've selected to run Add-Migration against.

    I hope this additional info helps! Also, please note, this same set of steps works just fine on another almost identical project.. that makes this so perplexing! Is there any way I can dig deeper to find out what is happening behind the scenes?

    Thursday, April 26, 2012 5:30 PM
  • 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

    • Marked as answer by two_o_six Monday, April 30, 2012 8:18 PM
    Friday, April 27, 2012 1:34 PM
  • I encountered the same problem. You just have to reference in your start up project the project that contains the DbContext class. Hope this helps
    • Marked as answer by two_o_six Monday, April 30, 2012 8:07 PM
    • Unmarked as answer by two_o_six Monday, April 30, 2012 8:08 PM
    • Marked as answer by two_o_six Monday, April 30, 2012 8:18 PM
    Saturday, April 28, 2012 7:13 AM
  • Hi paul206,

    Please try @RobdeT's and @peterparker44's solutions, they may help you to solve the issue.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, April 30, 2012 4:57 AM
    Moderator
  • Thanks for the help everyone. The answer was indeed that my startup project did not have a reference to the project containing the DbContext. Update-Database and Add-Migration now work just fine when I set my DbContext project as the startup project.

    I notice now when running with the -verbose flag that the startup project is logged, which could've helped provide this clue to me. I'd now recommend to others to use the -verbose flag when troubleshooting these types of errors.
    Monday, April 30, 2012 8:22 PM