添加数据库迁移时,"dotnet已停止工作"StackOverflowException

Ces*_*ion 6 c# entity-framework-core visual-studio-2015 asp.net-core

我有一个非常恼人的问题,每当我尝试向项目添加迁移时,dotnet崩溃并且不会创建迁移.无论我是否使用dotnet ef migrations add或,都会发生这种情况Add-Migration.命令开始运行并在必要时进行编译,然后崩溃并发生StackOverflowException.调试产生以下信息:

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

如果我的上下文有一个Dbset,其对象具有一个int属性,或者所有对象具有复杂属性和集合,则无关紧要.我可以生成迁移和快照的唯一情况是我的上下文没有DbSets.

我已经尝试了.NET Core的预发行版和发行版,以及完全卸载.NET Core SDK(因为仍然安装了旧版本)和Visual Studio并重新安装它们.

我在Windows 10 Pro上使用Visual Studio Enterprise 2015.3,我的模型类如下:

public class Player
{
    [Key]
    public int PlayerID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的背景如下:

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

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

    }

    public virtual DbSet<Player> Players { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和我的服务配置:

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

我的project.json,按要求:

{
  "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%" ]
  }
}
Run Code Online (Sandbox Code Playgroud)

Ces*_*ion 0

我想我明白了。虽然我确实将代码缩减为仅具有一个属性的一个集合,但在运行 Add-Migration 之前我没有保存它,因此它仍然使用更多的模型。它使用的看起来像这样:

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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

这确实有一个循环引用。我不知道为什么我没有想到这一点,尽管我认为案件没有得到处理有点奇怪,特别是因为这似乎是建立一对一关系的合乎逻辑的方式。不过回想起来,我记得没有在独立端使用ForeignKey属性,只是EF的工作原理。