简体   繁体   中英

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#.

We use EntityFramework 6 Code-First strategy to access a SQL Server 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.

We create a child of DbContext to deal with the EntityFramework context, like this:

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...).

And obviously we never enter the OnModelCreating method...

Again, the very same code works perfectly both locally and on 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:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM