[英]How to add a foreign key to ApplicationUser between separate project in EF Core?
在 .NET 5.0.0 中有一個帶有 Asp.Net Core 托管的 Blazor WebAssembly 應用程序,它獨立於 3 個不同的項目: WebApp.Client
、 WebApp.Server
、 WebApp.Shared
。
我正在使用AspNetCore.Identity
和IdentityServer4
來管理我的應用程序用戶。
但是 class ApplicationUser: IdentityUser
在WebApp.Server
項目中,而我的 model class Foo
在WebApp.Shared
項目中。 ( WebApp.Server
項目引用了WebApp.Shared
項目,因此Foo
無法using WebApp.Server.Models
。)
我可以在單獨的項目之間向ApplicationUser
class 添加外鍵嗎? 或者我可以將ApplicationUser
移動到WebApp.Shared
項目嗎?
// WebApp.Server.Models.ApplicationUser
public class ApplicationUser : IdentityUser
{
public virtual List<Foo> Foos { get; set; }
}
// WebApp.Shard.Models.Foo
public class Foo
{
public int ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; } // CS0246
}
一種沒有很多依賴的方法是:
// WebApp.Shard.Models.Foo
using System.ComponentModel.DataAnnotations.Schema;
public class Foo<TUser>
{
public int ApplicationUserId { get; set; }
[ForeignKey(nameof(ApplicationUserId))]
public virtual TUser ApplicationUser { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.