[英]Updating child entity EF core
有這個實體叫Service。 該實體有一個名為 TypeService 的子實體。 TypeService 可以是數據庫(MySQL)中的 null。
public class Service
{
public int Id { get; set; }
public string Name{ get; set; }
public string Department { get; set; }
public TypeService TypeService { get; set; }
}
MySQL:
`TypeServiceId` bigint(20) default NULL //(Colunm of Service table)
當我嘗試創建沒有 TypeService (TypeService == null) 的服務時,它工作正常。 當我嘗試編輯服務並包含 TypeService 時,它工作正常。 當我嘗試編輯服務並更改 TypeService 時,它工作正常。
我的問題是:當我嘗試更新服務並將 TypeService 設置為 null 時,EF 不會更新此 TypeServiceId
為什么 ef core update 將 TypeServiceId 字段正確更改為另一個值,但不更改為 null 值?
沒有錯誤信息。
謝謝和最好的問候!
據我了解,您想將 TypeServiceId 設置為 null 並且不在乎實體是否仍然存在於任何表中? 在這種情況下,您可以添加屬性
[Column(TypeName = "bigint(20)")]
public Int64? TypeServiceId { get; set; }
只需將 TypeServiceId 更改為 null,而不是實體本身。 確保查看遷移中的更改,以便 EntityFramework 不會創建新列,而不是使用創建的列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.