stack overflow保存資料時出錯dbContext.saveChanges()
Stack overflow.
Repeat 87208 times:
--------------------------------
at WebAPI.Models.WorkProfile..ctor()
at WebAPI.Models.WorkAccount..ctor()
--------------------------------
at DynamicClass..ctor()
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnTryRead(System.Text.Json.Utf8JsonReader ByRef, System.Type, System.Text.Json.JsonSerializerOptions, System.Text.Json.ReadStack ByRef, System.__Canon ByRef)
at System.Text.Json.Serialization.JsonConverter`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryRead(System.Text.Json.Utf8JsonReader ByRef, System.Type, System.Text.Json.JsonSerializerOptions, System.Text.Json.ReadStack ByRef, System.__Canon ByRef)
at System.Text.Json.Serialization.JsonConverter`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ReadCore(System.Text.Json.Utf8JsonReader ByRef, System.Text.Json.JsonSerializerOptions, System.Text.Json.ReadStack ByRef)
at System.Text.Json.Serialization.JsonConverter`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ReadCoreAsObject(System.Text.Json.Utf8JsonReader ByRef, System.Text.Json.JsonSerializerOptions, System.Text.Json.ReadStack ByRef)
etc.
etc.
etc..........
這是我的控制器:
[HttpPost]
[Consumes("application/json")]
public async Task<ActionResult<WorkAccount>> Post(WorkAccount workAccount)
{
if (Request.HasJsonContentType())
{
// this runs: logs out JSONcontent.
Console.WriteLine("JSON content");
}
else
{
Console.WriteLine("NO json!");
}
_context.WorkAccounts.Add(workAccount);
await _context.SaveChangesAsync();
return CreatedAtAction("GetTodoItem", workAccount);
}
這些是我的模型:
public class WorkAccount : BaseDateEntity
{
public int WorkAccountId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public WorkProfile? WorkProfile { get; set; } = new WorkProfile();
}
public class WorkProfile : BaseDateEntity
{
public int WorkProfileId { get; set; }
public int WorkAccountId { get; set; }
public List<Booking> Bookings { get; set; } = new List<Booking>();
public List<Advert> Adverts { get; set; } = new List<Advert>();
public List<WorkProfileLanguage> WorkProfileLanguages { get; set; } = new List<WorkProfileLanguage>();
public WorkAccount WorkAccount { get; set; } = new WorkAccount();
}
// and some more models...
fluentAPI 配置
public class WorkAccountConfig : IEntityTypeConfiguration<WorkAccount>
{
public void Configure(EntityTypeBuilder<WorkAccount> modelBuilder)
{
modelBuilder
.HasOne(x => x.WorkProfile)
.WithOne(y => y.WorkAccount)
.HasForeignKey<WorkProfile>(z => z.WorkAccountId);
}
}
在這個錯誤之前,我有另一個錯誤。我的 WorkProfile 導航屬性看起來像:public WorkProfile? WorkProfile { get; set; }沒有= new WorkProfile()初始化并產生以下錯誤:
System.NullReferenceException: Object reference not set to an instance of an object.
我也試過只回傳 JSON 資料,它不會產生錯誤和有效的 json。
public async Task<ActionResult<WorkAccount>> Post(WorkAccount workAccount)
{
return workAccount;
}
chrome 開發者工具中的 console.log 輸出。
{
"workAccountId": 0,
"email": "mail here",
"password": "boo",
"workProfile": null,
"createdAt": "0001-01-01T00:00:00",
"updatedAt": "0001-01-01T00:00:00"
}
我被困住了。有誰知道可能出了什么問題?
uj5u.com熱心網友回復:
任何時候 要么 要么WorkAccount被WorkProfile實體化,你將創建一個無限回圈,因為WorkAccount創建一個新的默認值WorkProfile,它創建一個新的默認值WorkAccount,它創建一個新的WorkProfile
我建議洗掉那些默認實體。
您可以通過嘗試實體化其中一個來獲得相同的行為。
using System;
public class Program
{
public static void Main()
{
var w = new WorkAccount();
}
public class WorkAccount
{
public int WorkAccountId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public WorkProfile? WorkProfile { get; set; } = new WorkProfile();
}
public class WorkProfile
{
public int WorkProfileId { get; set; }
public int WorkAccountId { get; set; }
public WorkAccount WorkAccount { get; set; } = new WorkAccount();
}
}
以上將導致
Stack overflow.
Repeat 130975 times:
--------------------------------
at Program WorkProfile..ctor()
at Program WorkAccount..ctor()
--------------------------------
at Program.Main()
Command terminated by signal 6
洗掉默認實體運行沒有問題。
using System;
public class Program
{
public static void Main()
{
var w = new WorkAccount();
}
public class WorkAccount
{
public int WorkAccountId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public WorkProfile? WorkProfile { get; set; }
}
public class WorkProfile
{
public int WorkProfileId { get; set; }
public int WorkAccountId { get; set; }
public WorkAccount WorkAccount { get; set; }
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/479114.html
