【问题标题】:Cannot create DB using EntityFramework 6 when the project is deployed on an IIS 8.5当项目部署在 IIS 8.5 上时,无法使用 EntityFramework 6 创建数据库
【发布时间】:2020-08-12 23:14:25
【问题描述】:

我有一个 Visual Studio 2019 项目,该项目使用 ASP.NET 和 C# 发布了一个 Web 应用程序 (web api)。

我们使用 EntityFramework 6 Code-First 策略来访问 SQL Server 数据库。

当我在本地或 Azure 服务上运行它时,一切都很好,但是当我将此 Web 应用程序部署到运行 Windows Server 2012 R2 的服务器中的 IIS 8.5 上时,由于某种原因 EntityFramework 6 无法创建数据库.

我们使用以下方法创建数据库:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MobileServiceContext, Migrations.Configuration>("MS_TableConnectionString"));

我们指向的是本地 SQL Server 2014。

我们创建一个 DbContext 的子级来处理 EntityFramework 上下文,如下所示:

public class MobileServiceContext : DbContext
{
    public const string CONNECTION_STRING_KEY = "MS_TableConnectionString"; // "MS_TableConnectionString"; 
    public const string CONNECTION_STRING_NAME = "Name=" + CONNECTION_STRING_KEY; // "MS_TableConnectionString"; 

    public MobileServiceContext() : base(CONNECTION_STRING_NAME)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      ... we add some logic here to create indexes etc.
    }
}

当我尝试在服务器上远程调试应用程序时,在第一次创建 MobileServiceContext 类型的对象时,执行并没有继续进行(我什至尝试在创建,但它没有进入异常捕获...)。

显然我们从来没有进入 OnModelCreating 方法...

同样,同样的代码在本地和 Azure 上都能完美运行。

我应该在我的服务器中配置什么吗?

【问题讨论】:

    标签: c# entity-framework entity-framework-6 ef-code-first


    【解决方案1】:

    为了记录,原来我必须在 web.config 中添加以下重定向:

    <dependentAssembly>
        <!--   Don't change this redirect!! It is part of a problem with a MS bug, see here https://github.com/dotnet/corefx/issues/25773 -->
        <assemblyIdentity name="System.Net.Http" publicKeyToken="B03F5F7F11D50A3A" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多