1

我想我的部署ASP.NET MVC5网站测试以下这个教程: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/deploying-to-iis无法在Web部署执行代码首先迁移

在关于“配置部署的应用程序数据库”的部分点2,我需要检查一个在我的屏幕上不可用的盒子(执行Code First Migrations)。我的问题如下:我有一个洋葱体系结构,因此我的包含实体框架上下文的项目位于一个单独的项目中(并且正在部署asp.net MVC 5项目),这意味着我不能在此项目中使用enable-migrations 。我也改变了我的连接字符串,以适应我的上下文的名称精简在这篇文章中:https://stackoverflow.com/a/32866872/4714502。另一方面,我的DbSet与我的实体框架类有不同的名称,这是否会成为问题?我这样做是因为我的表的语法非常尴尬,并且是一个要求(我不想在我的应用程序中使用这个命名约定)。假设一个表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 现在我走了想法,真的不知道我还能做什么?

回答

2

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

+0

我已经有app.config,它应该是完全相同的Web.config?做了这些步骤,但它不起作用。运行Update-database,我看不到要在'PMC'中运行的迁移。我将添加我的app.config,但Web.config更长 –

+0

@FlexabustBergson在''之后添加''。 PMC中标记为启动项目和默认项目吗? https://stackoverflow.com/a/26882750/3850405 – Ogglas

+0

是的,我确实标记了两个 –

相关问题