简体   繁体   English

当项目部署在 IIS 8.5 上时,无法使用 EntityFramework 6 创建数据库

[英]Cannot create DB using EntityFramework 6 when the project is deployed on an IIS 8.5

I have a Visual Studio 2019 project that publishes a web app (web api) using ASP.NET and C#.我有一个 Visual Studio 2019 项目,该项目使用 ASP.NET 和 C# 发布了 web 应用程序(web api)。

We use EntityFramework 6 Code-First strategy to access a SQL Server database.我们使用 EntityFramework 6 Code-First 策略来访问 SQL 服务器数据库。

When I run it locally or on an Azure Service, everything is great, but when I deploy this web app onto an IIS 8.5 in a server running Windows Server 2012 R2, for some reason EntityFramework 6 is not able to create the database. When I run it locally or on an Azure Service, everything is great, but when I deploy this web app onto an IIS 8.5 in a server running Windows Server 2012 R2, for some reason EntityFramework 6 is not able to create the database.

We create the database using the following:我们使用以下内容创建数据库:

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

We are pointing to a local SQL Server 2014.我们指向的是本地 SQL Server 2014。

We create a child of DbContext to deal with the EntityFramework context, like this:我们创建一个 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.
    }
}

When I try to remotely debug the app on the server, at the moment of creating for the first time an object of type MobileServiceContext, the execution just doesn't go ahead (I even tried to add a try-catch at the creation, but it doesn't enter into the Exception catch...).当我尝试在服务器上远程调试应用程序时,在第一次创建 MobileServiceContext 类型的 object 时,执行只是没有提前 go (我什至尝试在创建时添加 ZA75131CB1C90CDAFC1C0A52AE24Z,但它没有'不要进入异常捕获...)。

And obviously we never enter the OnModelCreating method...显然我们从来没有进入 OnModelCreating 方法......

Again, the very same code works perfectly both locally and on Azure.同样,相同的代码在本地和 Azure 上都能完美运行。

Should I configure anything in my server?我应该在我的服务器中配置任何东西吗?

Just for the records, it turned out I had to add the following redirection in the web.config:只是为了记录,事实证明我必须在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在IIS 8.5上使用HTTPS访问目录但无法使用HTTP时,找不到404服务器错误资源 - Server Error Resource cannot be found 404 when trying to access directory using HTTPS on IIS 8.5 but works using HTTP WebAssembly Blazor 在 IIS 上部署时不使用令牌 - WebAssembly Blazor not using token when deployed on IIS 使用EntityFramework时来自DB2的时间戳数据不准确 - Timestamp data from DB2 is not accurate when using EntityFramework 在子网站(IIS 8.5)中使用Unity:“无法创建抽象的InjectionMemberElement对象。” - Using Unity in child web site (IIS 8.5): “An abstract InjectionMemberElement object cannot be created.” 添加新绑定时,IIS 8.5会循环使用 - IIS 8.5 recycles when new bindings are added 使用URL重写IIS 8.5的wwwless网站 - wwwless site using url rewrite iis 8.5 无法阻止Windows Server 2012上的IIS 8.5中的回收 - Cannot prevent recycling in IIS 8.5 on windows server 2012 无法使用EntityFramework添加FK - Cannot add FK using EntityFramework 使用诊断解决方案模板时无法创建子项目 - Cannot create a sub project when using the Diagnostics solution template 文本框模式DateTimeLocal部署到IIS时不起作用 - Textbox Mode DateTimeLocal not working when deployed to IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM