简体   繁体   English

Database.EnsureCreated EF 7 ASP.NET 5 MVC 6处的堆栈溢出异常

[英]Stack Overflow exception at Database.EnsureCreated EF 7 ASP.NET 5 MVC 6

I am encountering an error at Database creation at Application Start whereas the exact same code works perfectly fine in all other projects. 我在应用程序启动时创建数据库时遇到错误,而完全相同的代码在所有其他项目中都能正常工作。

Startup function in Startup.cs 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() 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 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)
    {
    }
}

Error Screenshot 错误截图

在此处输入图片说明

After waiting for two days for an answer, unfortunately i ended up creating a new project and copying code there and it worked. 在等待了两天的答案之后,不幸的是我最终创建了一个新项目,并在那里复制了代码,并且它正常工作。 Seems like a configuration issue. 似乎是配置问题。

Note: Since i didn't received any answers i am marking this as the correct answer. 注意:由于我没有收到任何答案,因此将其标记为正确答案。 If a user comes in future and share their viewpoints, i will be happy to mark their answer if it adds some value to future readers. 如果用户将来有机会分享他们的观点,那么我会很乐意为他们的答案打上标记,如果它为将来的读者增加了一些价值。

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

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