我知道我的問題有點廣泛,但我還沒有在網上找到任何好的答案。
我正在構建一個 ASP.NET Core MVC Web 應用程式。我正在尋找有關如何將特定用戶系結到物體(模型中的類)的提示。問題實際上是關于如何將某個用戶系結到資料庫中的某個物體。
任何幫助將不勝感激。
uj5u.com熱心網友回復:
ApplicationUser(或IdentityUser) 就像任何其他物體類。
例如,創建 1-n 關系將類似于:
public class ApplicationUser : IdentityUser
{
public virtual IList<MyEntity> MyEntities { get; set; } // navigation property
}
public class MyEntity
{
public int Id { get; set; }
public string CreatedByUserId { get; set; } // FK
public ApplicationUser CreatedByUser { get; set; } // Navigation property
}
你也可以用流暢的方式做到這一點:
public class ApplicationUser : IdentityUser { }
public class MyEntity
{
public int Id { get; set; }
public string CreatedByUserId { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<MyEntity>(b => {
b.HasOne<ApplicationUser>()
.WithMany()
.HasForeignKey(myEntity => myEntity.CreatedByUser);
});
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/346741.html
標籤:实体框架 asp.net核心 asp.net-core-mvc
上一篇:有沒有更好的方法來概括具有相同屬性的dbSet-EntityFramework
下一篇:C++面向物件高級開發1
