[英]I want to add AspNetUsers table "Id" column as Foreign Key in another table
ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<Hospital> Hospitals { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
}
}
醫院.cs
public class Hospital : Common
{
[Key]
[Column(Order = 1)]
public int HospitalId { get; set; }
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Invalid e-mail address.")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[Display(Name = "Contact Person")]
public string ContactPerson { get; set; }
[Required]
[Display(Name = "Phone Number")]
[DataType(DataType.PhoneNumber)]
public string PhoneNum { get; set; }
[Required]
[MinLength(10)]
[Display(Name = "Mobile Number")]
public string MobileNum { get; set; }
[Required]
[Display(Name = "Address")]
public string Address { get; set; }
[Required]
[Display(Name = "Short Name")]
public string ShortName { get; set; } //Unique Slug name
}
Common.cs
public class Common
{
public DateTime? CreatedOn { get; set; }
public string CreatedBy { get; set; }
public DateTime? EditedOn { get; set; }
public string EditedBy { get; set; }
public DateTime? DeletedOn { get; set; }
public string DeletedBy { get; set; }
public bool? IsDeleted { get; set; } = false;
public bool? IsActive { get; set; } = true;
}
我想將Hospital表中AspNetUsers表的主鍵添加為具有這些列的外鍵:
由...制作
編輯者
刪除者
主要目標:所以使用這種關系,我想顯示帶有醫院詳細信息的用戶名,即創建/編輯/刪除醫院詳細信息。
使用數據注釋配置關系:在醫院 class 添加以下屬性:
public int AspNetUsersId{ get; set; }
[ForeignKey("AspNetUsersId")]
public virtual Common aspNetUser { get; set; }
在 Common class 中添加醫院屬性:
public virtual Hospital hospital { get; set; }
現在,您應該在 ApplicationDvContext 中定義此關系:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Hospital>()
.HasOptional(s => s.aspNetUser)
.WithRequiredPrincipal(ad => ad.hospital);
}
我不建議在兩個表之間超過一個關系。 我認為,這些列沒有必要建立關系。 Bcs這些colums只是信息coll。 但是您仍然可以像這樣編輯常見的 class 。
public class Common
{
public DateTime? CreatedOn { get; set; }
[ForeignKey("AspNetUsers")] public string CreatedBy { get; set; }
public AspNetUsers CreatedByUser { get; set; }
public DateTime? EditedOn { get; set; }
[ForeignKey("AspNetUsers")] public string EditedBy { get; set; }
public AspNetUsers EditedByUser { get; set; }
public DateTime? DeletedOn { get; set; }
[ForeignKey("AspNetUsers")] public string DeletedBy { get; set; }
public AspNetUsers DeletedByUser { get; set; }
public bool? IsDeleted { get; set; } = false;
public bool? IsActive { get; set; } = true;
}
您應該使用您想要的鍵關系從管理程序配置您的數據庫,然后從您的解決方案更新實體數據 model,否則實體將無法自行找到關系。 如果您使用的是微軟 sql 管理工作室,請查看這篇文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.