在生产环境中尝试 运行 EF 代码优先 Migrate.exe 工具失败

Trying to run EF Code first Migrate.exe tool on production environment failed

我正在使用 EntityFramework 版本 6.1.3。

我从EntityFramework版本6.1.3包工具文件夹复制migrate.exe到我网站的bin文件夹。

我的网站使用 x86 CPU 和目标框架 4.5.1

编译并发布到生产环境

我运行以下命令:

D:\Sites\MatrixPortal\bin>migrate.exe d:\sites\MatrixPortal\bin\MatrixPortal.BL.dll /startUpDirectory="d:\Sites\MatrixPortal\bin" /startUpConfigurationFile="d:\Sites\MatrixPortal\Web.config"

我收到以下错误:

ERROR: Could not load file or assembly 'd:\sites\MatrixPortal\bin\MatrixPortal.BL' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

编辑:

当我尝试这个时:

migrate MatrixPortal.BL.dll /verbose

我收到其他错误:

System.BadImageFormatException: Could not load file or assembly 'MatrixPortal.BL' or one of its dependencies. An attempt was made to load a program with an incorrect format. File name: 'MatrixPortal.BL' at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) 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.LoadAssembly(String name) at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration() at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.Data.Entity.Migrations.Console.Program.Run() at System.Data.Entity.Migrations.Console.Program.Main(String[] args)

=== Pre-bind state information === LOG: DisplayName = MatrixPortal.BL (Partial) WRN: Partial binding information was supplied for an assembly: WRN: Assembly Name: MatrixPortal.BL | Domain ID: 2 WRN: A partial bind occurs when only part of the assembly display name is provided. WRN: This might result in the binder loading an incorrect assembly. WRN: It is recommended to provide a fully specified textual identity for the assembly, WRN: that consists of the simple name, version, culture, and public key token. WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue. LOG: Appbase = file:///D:/Sites/MatrixPortal/bin/ LOG: Initial PrivatePath = NULL Calling assembly : EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. === LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Users\guy_b\AppData\Local\Temp\tmpC402.tmp LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). LOG: Attempting download of new URL file:///D:/Sites/MatrixPortal/bin/MatrixPortal.BL.DLL. ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

ERROR: Could not load file or assembly 'MatrixPortal.BL' or one of its dependencies. An attempt was made to load a program with an incorrect format.

也试过这个: http://www.azurefromthetrenches.com/command-line-entity-framework-code-first-migrations/

并创建了一个名为 Redirect.config 的文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
                                <bindingRedirect oldVersion="0.0.0-6.1.3" newVersion="6.1.3" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

但是得到了和上面一样的错误:(

已解决!

我已经发布了目标为 x86 的网站项目 cpu,显然它不是真正的 x86,我不知道为什么,也许是一个错误?

将 Publish 更改为任何 cpu 后它起作用了。我能够 运行 migrate.exe 使用以下参数没有错误:

migrate.exe MatrixPortal.BL.dll /verbose /startUpDataDirectory="D:\Sites\MatrixPortal" /startUpConfigurationFile="D:\Sites\MatrixPortal\Web.config"

在我的例子中,我得到了错误,因为我写了 migrate .\MyAssembly.dll 而不是 migrate MyAssembly.dll

请参阅 Common Problems 文档部分。