为 Entity FrameWork 中的多种用途映射同一模型 class

Map same Model class for multiple purposes in Entity FrameWork

我有两个型号类,一个是ApplicationUser,另一个是Appointment。应用程序用户包括使用该应用程序的所有用户,在我的例子中是医生和数据输入操作员。每次预约都会分配医生,数据输入操作员会将此日志记录到数据库中。我想通过预约映射这两个用户。我试过这样的东西

public class Appointment
{
    public int AppointmentID { get; set; }
    public DateTime Date { get; set; }

    public int DoctorID { get; set; }
    [ForeignKey("DoctorID")]
    public virtual ApplicationUser Doctor { get; set; }

    public int SystemUserID { get; set; }
    public virtual ApplicationUser SystemUser { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public string Email { get; set; }
    public string Mobile { get; set; }
    public string FirstNsme { get; set; }
    public string LastName { get; set; }
}

但这会引发错误

Appointment_Doctor_Target_Appointment_Doctor_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'DoctorID' on entity 'Appointment' does not match the type of property 'Id' on entity 'ApplicationUser' in the referential constraint 'Appointment_Doctor'.

谁能指出为什么会出现此错误以及解决此问题的正确方法是什么?

IdentityUser,因为 asp.net 身份 entity framework 中的所有实体都将 string 作为键。您正在尝试映射到 int。所以要么在你的 Appointment 实体中使用 Guids 作为外键

public class Appointment
{
    [Key]
    public int AppointmentID { get; set; }
    public DateTime Date { get; set; }

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

    public string SystemUserID { get; set; }
    [ForeignKey("SystemUserID ")]
    public virtual ApplicationUser SystemUser { get; set; }
}

或者将identity 类中Ids的类型改为int。您可以找到帮助 here.

您的 classes 中存在多个问题。

什么是医生ID?在哪里定义的?

您需要首先关注在逻辑上建立实体之间的正确关系。

我认为您的约会 class 不需要包含添加约会的 SystemUserID。

其次,如果您想在两种用户类型之间共享一些属性,而不是创建一个公共 class 并在 Doctor 和 SystemUser 中派生。

将 DoctorId 添加到 Doctor table 以及与 Doctor 有关的特定详细信息,例如专业.

SystemUser 添加约会,因此 table 应包含与之相关的数据,即 doctorId 和 appointmentId。

更新:

根据您的评论,您可以这样做。注意仅供参考,你是更好的人来定义更好的DB Schema。

public class Appointment
{
    public int AppointmentID { get; set; }
    public DateTime Date { get; set; }

    public int DoctorID { get; set; }
    [ForeignKey("ApplicationUserId")]
    public virtual ApplicationUser Doctor { get; set; }

    public int SystemUserID { get; set; }
    [ForeignKey("ApplicationUserId")]
    public virtual ApplicationUser SystemUser { get; set; }
}

public class ApplicationUser
{
    public int ApplicationUserId { get; set; }
    public string Email { get; set; }
    public string Mobile { get; set; }
    public string FirstNsme { get; set; }
    public string LastName { get; set; }
    public UserType UserType { get; set; }
}

public enum UserType
{
    Doctor,
    SystemUser
}

更多更复杂的错误:

我在 4 个链接的 table 中多次遇到此错误。

每个 table 都有 3 - 7 个字段的复合键, 并且一个 table 引用了它自己的 3 字段键和它自己的列的不同组合。

我花了很长时间修复一个字段序列(确实修复了其他帖子中提到的错误),结果在其他实体中产生了连锁反应。

解决方法: 按减少出现的顺序对齐所有链接的 table 的 FK 字段

最初:

关键字段对齐后:

并对 DbContext 中 FluentAPI 中的所有匿名 FK 对象重新排序以匹配新顺序。

这解决了所有令人头疼的问题。