所以每種類型的表會爲你的模式並不完全工作,因爲每種類型的表一起通過共享主鍵連接表。您可以修改您的模式,以便UserAccounts,ExternalUserAccounts和SystemUserAccounts都在UserAccountId上鍵入。然後下面的代碼接近你想要的。否則,你會想要使用常規的關聯特性。
閱讀關於每here類型表和共享主鍵here。
public class UserAccount
{
public int UserAccountId {get;set;}
public int UserId {get;set;}
public DateTime LastLogin {get;set;}
public bool IsLocked {get;set;}
public bool IsActive {get;set;}
public User User {get;set;}
}
public class ExternalUserAccount : UserAccount
{
public int ExternalAccountId {get;set;}
public string ProviderName {get;set;}
public string ProviderUserName {get;set;}
}
public class SystemUserAccount : UserAccount
{
public int SystemUserAccountId {get;set;}
public string PasswordHash {get;set;}
public string Token {get;set;}
}
public class User
{
public int UserId {get;set;}
public string Name {get;set;}
public string Description {get;set;}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<UserAccount>().ToTable("UserAccounts");
modelBuilder.Entity<UserAccount>().HasKey(ua => ua.UserAccountId);
modelBuilder.Entity<UserAccount>().HasRequired(ua => ua.User)
.WithMany()
.HasForeignKey(ua => ua.UserId);
modelBuilder.Entity<ExternalUserAccount>().ToTable("ExternalUserAccounts");
modelBuilder.Entity<SystemUserAccount>().ToTable("SystemUserAccounts");
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<User>().HasKey(u => u.UserId);
}
所以。要做到這一點的唯一方法是將第一級繼承替換爲一對一關係? –
是的,他們必須共享一個主鍵。看到我更新的答案。 –