【问题标题】:"dotnet has stopped working" StackOverflowException when adding database migration添加数据库迁移时出现“dotnet 已停止工作”StackOverflowException
【发布时间】:2016-10-30 21:56:55
【问题描述】:

我遇到了一个非常烦人的问题,每次我尝试向我的项目添加迁移时,dotnet 都会崩溃并且不会创建迁移。无论我使用dotnet ef migrations add 还是Add-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%" ]
  }
}

【问题讨论】:

  • 可以发布project.json,希望每个引用都是正确的。
  • @Anuraj 我已将其添加到帖子中。
  • 奇怪。它在我的系统中没有任何问题。我正在使用 Yoman 生成的 ASP.NET Web App。仅更改我使用的是硬编码的连接字符串,而不是从配置中读取。希望您使用的是 1.0.0-preview2-003121 版本的 dotnet.exe。
  • @Anuraj 是的,dotnet --version 提供 1.0.0-preview2-003121。你认为还有什么不同的地方吗?
  • 并非一切都一样,我使用的是最新更新的 Windows 10 Pro。您使用的是 Visual Studio 还是命令行?

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


【解决方案1】:

我想我明白了。虽然我确实将我的代码缩减为只有一个具有一个属性的集合,但在运行 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 是如何工作的。

【讨论】:

    猜你喜欢
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多