iT邦幫忙

0

請問如何使用EntityFrameworkCore把 Foreign key外键 設置為可選的?

  • 分享至 

  • xImage
public class ApplicationUser:IdentityUser
{
    
    // ForeignKey設置為可選
    [ForeignKey("Hometown")]
    public int? HometownId { get; set; }
    public Hometown Hometown { get; set; }
}

為什麼這樣執行migration:

dotnet ef migrations add CreateForeignKey
dotnet ef database update

後HometownId的欄位還是不能為空Null?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2023-04-30 09:15:04

參考這篇看看合不合用

1
科科
iT邦研究生 1 級 ‧ 2023-05-02 09:12:34

改成這樣:

public class ApplicationUser:IdentityUser
{
    public int? HometownId { get; set; }
    
    // ForeignKey設置為可選
    [ForeignKey("HometownId")]
    public virtual Hometown Hometown { get; set; }
}

我要發表回答

立即登入回答