簡體   English   中英

如何在 EF Core 中的不同項目之間向 ApplicationUser 添加外鍵?

[英]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.ClientWebApp.ServerWebApp.Shared

我正在使用AspNetCore.IdentityIdentityServer4來管理我的應用程序用戶。

但是 class ApplicationUser: IdentityUserWebApp.Server項目中,而我的 model class FooWebApp.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM