【问题标题】:Can't Execute Code First Migrations in Web Deploy无法在 Web 部署中执行代码优先迁移
【发布时间】:2017-09-14 08:21:44
【问题描述】:

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

在关于“为应用程序数据库配置部署”第 2 点的部分中,我需要选中一个在我的屏幕上不可用的框(执行代码优先迁移)。我的问题如下:我有一个洋葱架构,因此我的项目包含实体框架的上下文是在一个单独的项目中(我正在部署 asp.net MVC 5 项目),这意味着我不能enable-migrations in这个项目。我还更改了我的连接字符串以适合我在这篇文章中精确描述的上下文名称:https://stackoverflow.com/a/32866872/4714502。另一方面,我的 DbSet 与我的实体框架类有不同的名称,这会是一个问题吗?我这样做是因为我的表的语法很尴尬,而且是一个要求(我不想在我的应用程序中使用这个命名约定)。说一个表 Web_Documents :

DbSet<Web_Documents> WebDoc { get; set; }

至于我的架构:

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

依赖关系从 4 变为 1。

这是我在 Repository 中的 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 现在我没有想法了,真的不知道我还能做什么?

【问题讨论】:

    标签: c# asp.net-mvc entity-framework visual-studio-2017 webdeploy


    【解决方案1】:

    在包含上下文的项目中创建一个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,然后运行更新数据库。现在它会更新。

    【讨论】:

    • 我已经有了app.config,应该和Web.config一模一样吗?做了这些步骤,但它不起作用。运行 Update-database ,我看不到要在 PMC 中运行的迁移。我将添加我的 app.config,但 Web.config 更长
    • @FlexabustBergson 在&lt;/configSections&gt; 之后添加&lt;connectionStrings&gt;。您是否在 PMC 中标记为启动项目和默认项目? stackoverflow.com/a/26882750/3850405
    • 运行Update-Database时收到什么错误信息?
    • 没有错误,只是说没有要运行的显式迁移(它是法语)。
    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2013-05-21
    • 2018-07-02
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多