Search code examples
c#entity-frameworkasp.net-coreasp.net-core-2.0asp.net-core-mvc-2.0

AspNetCore 2.0 crashing when calling EntityFrameWork core (Overflow Exception)


I have had this problem for a while now, and have talked to Microsoft about it, with no solution so far. Now I have reproduced the error in a brand new project. I have removed all unnessary code, and this code is still giving me the error:

System.StackOverflowException occurred
  HResult=0x800703E9
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

My project consists of a Startup.cs class with this content:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<FrilivDB>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")).EnableSensitiveDataLogging());
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc(routes =>
        {
            routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
            routes.MapRoute("ViewProduct", "{brand}/{productTitle}", defaults: new { controller = "Products", action = "Details" });
        });
    }
}

My Productscontroller looks like this:

public class ProductsController : Controller
{
    private readonly FrilivDB _context;

    public ProductsController(FrilivDB context)
    {
        _context = context;
    }

    public async Task<IActionResult> Details(string brand, string productTitle)
    {
        var product = _context.Products.Select(p => p.Title);
        return View();
    }
}

And when passing by this line in the debugger the error occurs:

var product = _context.Products.Select(p => p.Title);

The error occurs after waiting between 15 and 30 seconds, every time. Any one has any ideas or experienced the same thing?

This is what I see in the Output window:

    AO.Shop>       Request starting HTTP/1.1 GET http://localhost:44317/tatonka/alaska  
AO.Shop> info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
AO.Shop>       Request starting HTTP/1.1 GET http://localhost:44317/tatonka/alaska  
AO.Shop> info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
AO.Shop>       Executing action method AO.Shop.Controllers.ProductsController.Details (AO.Shop) with arguments (tatonka, alaska) - ModelState is Valid
AO.Shop> info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
AO.Shop>       Executing action method AO.Shop.Controllers.ProductsController.Details (AO.Shop) with arguments (tatonka, alaska) - ModelState is Valid
AO.Shop> 
AO.Shop> Process is terminating due to StackOverflowException.

Solution

  • After a lot of comminication with Microsoft and a whole lot of debugging and commenting code in and out, I found the problem.

    It was due to poor DB settings. I had 4 tables in my DB which were referencing them selves as Foreign keys, that gave a Stackoverflow Exception from the EF Core.

    After removing all 4 and rebuilding the entire Model, it worked. Thanks for your input