添加數據庫遷移時,“dotnet已停止工作”StackOverflowException

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

我有一個非常煩人的問題,每當我嘗試向項目添加遷移時,dotnet崩潰並且不會創建遷移。無論我是否使用dotnet ef migrations addAdd-Migration都會發生這種情況。該命令開始運行並在必要時進行編譯,然後它與StackOverflowException崩潰。調試產生以下信息:

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

如果我的上下文有一個Dbset,其對象具有一個int屬性或所有對象具有復雜屬性和集合,則無關緊要等等。我可以生成遷移和快照的唯一情況是我的上下文沒有DbSet。

我已經嘗試了.NET Core的預發行版和發行版,以及完全卸載.NET Core SDK(因為仍然安裝了舊版本)和Visual Studio並重新安裝它們。

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

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

我的背景如下:

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

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

    }

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

和我的服務配置:

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

我的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%" ]
  }
}

熱門答案

我想我想通了。雖然我確實將我的代碼減少到只有一個屬性的集合,但我沒有在運行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; }
}

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; }
}

確實有一個循環參考。我不知道為什麼我沒有想到這一點,雖然我覺得有些奇怪的是案件沒有處理,特別是因為這似乎是建立一對一關係的合理方式。回想一下,我記得不要在獨立方面使用ForeignKey屬性,而是EF如何工作。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因