简体   繁体   中英

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

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)
    {
    }
}

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.

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