Database.EnsureCreated EF 7 ASP.NET 5 MVC 6上的堆棧溢出異常

asp.net asp.net-mvc c# entity-framework entity-framework-core

我在Application Start創建數據庫時遇到錯誤,而完全相同的代碼在所有其他項目中完全正常。

Startup.cs中的啟動功能

public Startup(IHostingEnvironment env)
{
    // Set up configuration sources.
    var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();

    if (env.IsDevelopment())
    {
        // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
            builder.AddApplicationInsightsSettings(developerMode: true);
    }
    Configuration = builder.Build();
    Globals.Configuration = Configuration;
    Globals.HostingEnvironment = env;
    Globals.EnsureDatabaseCreated();
}

Globals.EnsureDatabaseCreated()

public static void EnsureDatabaseCreated()
    {
        var optionsBuilder = new DbContextOptionsBuilder();
        if (HostingEnvironment.IsDevelopment()) optionsBuilder.UseSqlServer(Configuration["Data:dev:DataContext"]);
        else if (HostingEnvironment.IsStaging()) optionsBuilder.UseSqlServer(Configuration["Data:staging:DataContext"]);
        else if (HostingEnvironment.IsProduction()) optionsBuilder.UseSqlServer(Configuration["Data:live:DataContext"]);
        var context = new ApplicationContext(optionsBuilder.Options);
        context.Database.EnsureCreated();

        optionsBuilder = new DbContextOptionsBuilder();
        if (HostingEnvironment.IsDevelopment()) optionsBuilder.UseSqlServer(Configuration["Data:dev:TransientContext"]);
        else if (HostingEnvironment.IsStaging()) optionsBuilder.UseSqlServer(Configuration["Data:staging:TransientContext"]);
        else if (HostingEnvironment.IsProduction()) optionsBuilder.UseSqlServer(Configuration["Data:live:TransientContext"]);
        new TransientContext(optionsBuilder.Options).Database.EnsureCreated();
    }

ApplicationContext.cs

public class ApplicationContext : DbContext
{
    public DbSet<Models.Security.User> Logins { get; set; }
    public DbSet<Models.Security.Session> Sessions { get; set; }
    public DbSet<Models.Security.Verification> VerificationTokens { get; set; }

    public DbSet<Models.CRM.User> Users { get; set; }
    public DbSet<Models.CRM.Organization> Merchants { get; set; }
    public DbSet<Models.CRM.LinkedAddress> Shops { get; set; }
    public DbSet<Models.CRM.ContactDetail> ContactDetails { get; set; }
    public DbSet<Models.CRM.Location> Locations { get; set; }

    public ApplicationContext(DbContextOptions options) : base(options)
    {
    }
}

錯誤截圖

在此處輸入圖像描述

熱門答案

等了兩天才得到答案,不幸的是我最終創建了一個新項目,並在那裡複製代碼並且工作正常。看起來像配置問題。

注意:由於我沒有收到任何答案,我將此標記為正確答案。如果用戶將來並分享他們的觀點,我將很樂意標記他們的答案,如果它為未來的讀者增加了一些價值。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因