简体   繁体   English

身份新表外键AspNetUsers

[英]Identity new table foreign key to AspNetUsers

I am creating a new table (Let's call it Chart) under MVC Identity which consists of 2 columns (PatientId and DoctorId) that will refer back to the Id column of AspNetUsers. 我正在MVC Identity下创建一个新表(我们称之为Chart),该表由2列(PatientId和DoctorId)组成,这些列将返回AspNetUsers的Id列。 The new table will also have a PK of its own. 新表也将具有其自己的PK。 The following is the IdentityModels class. 以下是IdentityModels类。

public class ApplicationUser : IdentityUser 
{
  public virtual Chart Chart { get; set; }     
  ...
}

public class Chart 
{
   [Key]
   public int Id { get; set; } 

   //How to FK the following two params?
   public string PatientId { get; set; }
   public string DoctorId { get; set; }

   public virtual ApplicationUser User { get; set; } // navigation property    
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{
    public ApplicationDbContext()
          : base("DefaultConnection", throwIfV1Schema: false) {
}

public System.Data.Entity.DbSet<Chart> Chart { get; set; }

public static ApplicationDbContext Create()
{
   return new ApplicationDbContext();
}

May I know how I can refer both my PatientId and DoctorId back to the Id column of AspNetUsers table? 我可以知道如何将我的PatientId和DoctorId都返回到AspNetUsers表的Id列吗?

It will be a one-to-many relationship (One DoctorId can have many PatientId, but one PatientId can only be attached to one DoctorId). 这将是一对多的关系(一个DoctorId可以有多个PatientId,但是一个PatientId只能附加到一个DoctorId)。

If I'm correct in assuming that both Patient and Doctor are in fact "users", then you should actually inherit them from ApplicationUser . 如果我正确地假设PatientDoctor实际上都是“用户”,那么您实际上应该从ApplicationUser继承它们。

public class Patient : ApplicationUser
{
    // patient-specific properties

    [ForeignKey("Doctor")]
    public string DoctorId { get; set; } // IdentityUser's PK is string by default
    public virtual Doctor Doctor { get; set; }
}

public class Doctor : ApplicationUser
{
    // doctor-specific properties

    public virtual ICollection<Patient> Patients { get; set; }
}

By default, Entity Framework employs single-table inheritance, so in this configuration, you won't actually end up with separate tables for Doctor and Patient . 默认情况下,Entity Framework采用单表继承,因此在此配置中,您实际上不会最终获得DoctorPatient单独表。 Instead, all the properties of both will be added to AspNetUsers . 而是将两者的所有属性都添加到AspNetUsers For the most part, this is not an issue. 在大多数情况下,这不是问题。 The only time it can be problematic is if you need to require a property specific to just one subclass, such as Doctor . 唯一有问题的时间是,您是否需要只针对一个子类的属性,例如Doctor All properties on subclasses in this configuration must be nullable, since there is logically no way to provide a required value for Doctor when saving Patient . 此配置中子类的所有属性都必须为空,因为从逻辑上讲,在保存Patient时无法为Doctor提供所需的值。 However, this is only enforced at the database-level. 但是,这仅在数据库级别强制执行。 You're still free to validate an input in a form, for example, as being required, even if the table-column backing it is not. 您仍然可以自由验证表单中的输入(例如,根据需要),即使不需要输入的表列也是如此。

That said, there are other strategies you can use. 也就是说,您还可以使用其他策略。 In this case, the most appropriate alternative would be TPT, or Table-Per-Type. 在这种情况下,最合适的替代方法是TPT或Table-Per-Type。 Here, you get a table for each discrete type, ApplicationUser , Doctor, and Patient . 在这里,您可以获得每种离散类型的表格ApplicationUserDoctor,Patient Then, on the subclasses ( Doctor and Patient ) a foreign key is added to ApplicationUser . 然后,在子类( DoctorPatient )上,将外键添加到ApplicationUser It is that ApplicationUser instance that holds the true "id" for the entity. 正是那个ApplicationUser实例保存了实体的真实“ id”。 To use TPT, it's as simple as adding the Table attribute to each class: 要使用TPT,就像将Table属性添加到每个类一样简单:

[Table("Doctors")]
public class Doctor : ApplicationUser

[Table("Patients")]
public class Patient : ApplicationUser

UPDATE 更新

Regarding Chart , with this setup, your implementation would look like: 关于Chart ,使用此设置,您的实现将如下所示:

public class Chart 
{
    [Key]
    public int Id { get; set; } 

    [ForeignKey("Patient")]
    public string PatientId { get; set; }
    public virtual Patient Patient { get; set; }

    [ForeignKey("Doctor")]
    public string DoctorId { get; set; }
    public virtual Doctor Doctor { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 dbo.AspNetUsers 表上的 ASP.NET MVC 5 核心标识 CompanyID 外键 - ASP.NET MVC 5 Core Identity CompanyID foreign key on dbo.AspNetUsers table AspNetUsers的主键作为另一个表的外来 - Primary key of AspNetUsers as foreign of another table Identity 3.0 - 如何在分层应用程序中使用 AspNetUsers ID 作为外键 - Identity 3.0 - How to use AspNetUsers ID as Foreign Key in a Tiered Application AspNetUsers的ID作为单独表中的外键,一对一的关系 - AspNetUsers' ID as Foreign key in separate table, one-to-one relationship 我想在另一个表中添加 AspNetUsers 表“Id”列作为外键 - I want to add AspNetUsers table "Id" column as Foreign Key in another table 将标识列添加到 AspNetUsers 表 MVC Core - Add Identity column to AspNetUsers table MVC Core 具有标识用户外键的新表 - New table with a foreign key to identityuser 在表 &#39;Tickets&#39; 上引入 FOREIGN KEY 约束 &#39;FK_dbo.Tickets_dbo.AspNetUsers_UserID&#39; 可能会导致循环或多个级联路径 - Introducing FOREIGN KEY constraint 'FK_dbo.Tickets_dbo.AspNetUsers_UserID' on table 'Tickets' may cause cycles or multiple cascade paths 在表 'Jobs' 上引入 FOREIGN KEY 约束 'FK_Jobs_AspNetUsers_UserId' 可能会导致循环或多个级联路径 - Introducing FOREIGN KEY constraint 'FK_Jobs_AspNetUsers_UserId' on table 'Jobs' may cause cycles or multiple cascade paths 如何在 mvc5 中的 AspNetUsers 中添加外键引用 - how to add a foreign key referance in AspNetUsers in mvc5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM