无法在 Web 部署中执行代码优先迁移

Can't Execute Code First Migrations in Web Deploy

我正在尝试部署我的 ASP.NET MVC5 网站以按照本教程进行测试: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/deploying-to-iis

在关于 "Configure deployment for the application database" 第 2 点的部分,我需要选中屏幕上不可用的框(执行代码优先迁移)。我的问题如下:我有一个洋葱架构,因此我的项目包含 Entity Framework 的上下文是在一个单独的项目中(我正在部署 asp.net MVC 5 项目),所以这意味着我不能 enable-migrations 在此项目中。我还更改了我的连接字符串以适合我的上下文名称,如 post : 中所精确。另一方面,我的 DbSet 的名称与 Entity Framework 类 不同,这会是个问题吗?我这样做是因为我的 tables 的语法很笨拙,并且是一个要求(我不想在我的应用程序中使用这个命名约定)。说 table Web_Documents :

DbSet<Web_Documents> WebDoc { get; set; }

至于我的架构:

  1. 实体 (POCO)
  2. 存储库(上下文在这里
  3. 服务
  4. UI (MVC)(在此处部署

依赖关系从 4 变为 1。

这是我在存储库中的 app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="PrincipalServerContext" connectionString="Data Source=SMRFG12APP13\SQLEXPRESS;Initial Catalog=PrincipalServerDB;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

我的数据库名称是 PrincipalServerDB,上下文是 PrincipalServerContext 现在我没主意了,真的不知道我还能做什么?

在包含上下文的项目中创建一个 App.config 文件,并在其中添加您的 connectionString。匹配 Web.config 结构。示例:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=YourDb Security=sspi;Pooling=false;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

将上下文项目设置为 StartUp Project 之后,在 PMC 中将其设置为 Default project 并在此之后 运行 更新数据库。现在它会更新。