"dotnet has stopped working" StackOverflowException when adding database migration

asp.net-core c# entity-framework-core visual-studio-2015

Question

I'm having a really annoying issue where every time I try to add a migration to my project, dotnet crashes and the migration is not created. This happens regardless of whether I use dotnet ef migrations add or Add-Migration. The command begins to run and compilation occurs if necessary, then it crashes with a StackOverflowException. Debugging yields the following information:

Unhandled exception at 0x00007FFF798C97DE (coreclr.dll) in dotnet.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000AC03A75FF8).

It also doesn't matter if my context has one Dbset whose objects have one int property or all objects with their complex properties and collections, et cetera. The only case where I can generate a migration and snapshot is if my context has no DbSets.

I have tried both the prerelease and release versions of .NET Core, as well as fully uninstalling the .NET Core SDK (as there were old versions still installed) and Visual Studio and reinstalling them.

I am using Visual Studio Enterprise 2015.3 on Windows 10 Pro and my model class is below:

public class Player
{
    [Key]
    public int PlayerID { get; set; }
}

and my context is as follows:

public class LeagueContext : DbContext
{
    public LeagueContext(DbContextOptions<LeagueContext> context) : base(context)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

    }

    public virtual DbSet<Player> Players { get; set; }
}

and my service configuration:

services.AddEntityFrameworkSqlServer().AddDbContext<LeagueContext>(config =>
        {
            config.UseSqlServer(Configuration["ConnectionStrings:LeagueContext"]);
        });

My project.json, as requested:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "Microsoft.EntityFrameworkCore": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final"
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}
1
6
6/29/2016 12:35:27 PM

Popular Answer

I think I figured it out. While I did cut down my code to just the one collection with one property, I didn't save it before running Add-Migration so it was still using more of the model. What it was using looked like this:

public class Player
{
    [Key]
    public int PlayerID { get; set; }

    public int OrganizationID { get; set; }

    public int CurrentTeamID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public DateTime JoinDate { get; set; }

    [ForeignKey("PlayerID")]
    public virtual PlayerAccount Account { get; set; }

    //[ForeignKey("PlayerID")]
    //public virtual PlayerCareer Career { get; set; }

    //[ForeignKey("PlayerID")]
    //public virtual PlayerInfo Info { get; set; }
}

and

public class PlayerAccount
{
    [Key]
    public int PlayerID { get; set; }

    public string Category { get; set; }

    public bool LeagueEmails { get; set; }

    public bool GameReminders { get; set; }

    [ForeignKey("PlayerID")]
    public virtual Player Player { get; set; }
}

which indeed has a circular reference. I don't know why I didn't think of that, though I think it is a bit odd that case isn't handled, especially as this seems like a logical way to set up a one-to-one relationship. Thinking back though, I remember not to use the ForeignKey attribute on the independent side, just how EF works.

0
6/29/2016 2:11:21 PM


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