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

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

Question

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

enter image description here

1
0
3/29/2016 7:19:35 PM

Popular Answer

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.

0
3/12/2016 7:11:51 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow