我遇到了一个非常恼人的问题,每当我尝试向项目添加迁移时,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属性,或者所有对象具有复杂属性和集合,则无关紧要等等。我可以生成迁移和快照的唯一情况是我的上下文没有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; }
}
我的背景如下:
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%" ]
}
}
答案 0 :(得分: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; }
}
和
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如何工作。