zoukankan      html  css  js  c++  java
  • EF Code First AddMigration fails with Could not load file or assembly error

    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

  • 相关阅读:
    C#新功能--命名参数与可选参数
    C#新功能--命名参数
    初识SqlLite ---.net连接数据库
    混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该...
    看到他我一下子就悟了-- 泛型(2)
    ExtJS4中设置tabpanel的tab高度问题
    java学习1-环境搭建
    oracle获取clob调优
    oracle job定时执行存储过程
    【转】【真正福利】成为专业程序员路上用到的各种优秀资料、神器及框架
  • 原文地址:https://www.cnblogs.com/philzhou/p/2492076.html
Copyright © 2011-2022 走看看