C# 在实体框架中为多种目的映射同一模型类

C# 在实体框架中为多种目的映射同一模型类,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我有两个模型类,一个是ApplicationUser,第二个是Appointment。应用程序用户包括使用该应用程序的所有用户,在我的例子中,包括医生和数据输入操作员。将为每次预约指定医生,数据输入操作员将向DB记录此日志。我想将这两个用户映射为约会。我试过这样的东西 public class Appointment { public int AppointmentID { get; set; } public DateTime Date { get; set; } p

我有两个模型类,一个是
ApplicationUser
,第二个是
Appointment
。应用程序用户包括使用该应用程序的所有用户,在我的例子中,包括医生和数据输入操作员。将为每次预约指定医生,数据输入操作员将向DB记录此日志。我想将这两个用户映射为约会。我试过这样的东西

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; }
}
但这是一个错误

约会\医生\目标\约会\医生\源::引用约束的从属角色中所有属性的类型必须与主体角色中相应的属性类型相同。实体“约会”上的属性“DoctorID”的类型与引用约束“约会\医生”中实体“ApplicationUser”上的属性“Id”的类型不匹配


有人能指出发生此错误的原因以及解决此问题的正确方法吗?

IdentityUser,因为asp.net identity entity framework中的所有实体都将
字符串作为键。您正在尝试映射到
int
。因此,在约会实体中使用guid作为外键

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类中的ID类型更改为int。您可以找到帮助。

您的类中存在多个问题

什么是博士?它的定义在哪里

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

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

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

在医生表中添加医生ID以及与医生相关的具体细节,如专业

SystemUser添加约会,因此表中应包含与该约会相关的数据,即doctorId和appointmentId

更新:

根据你的评论,你可以这样做。请注意,它仅供参考,您最好定义一个更好的DB模式

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个链接表中多次出现此错误

每个表都有3-7个字段的复合键, 一个表引用了自己的3字段键,并使用了不同的列组合

多年来,我一直在努力修复一个字段序列(它确实修复了其他帖子中提到的错误),结果却在其他实体中产生了连锁反应

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

原来:

对齐关键字段后:

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


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

现在错误变为此。约会\医生\目标\约会\医生\源::引用约束的从属角色中所有属性的类型必须与主体角色中相应的属性类型相同。实体“约会”上的属性“DoctorID”的类型与引用约束“约会\医生”中实体“ApplicationUser”上的属性“Id”的类型不匹配。预约\系统用户\目标\预约\系统用户\来源:。。。。。约会\医生\目标\约会\医生\源::引用约束的从属角色中所有属性的类型必须与主体角色中相应的属性类型相同。实体“约会”上的属性“DoctorID”的类型与引用约束“约会\医生”中实体“ApplicationUser”上的属性“Id”的类型不匹配。约会\系统用户\目标\约会\系统用户\来源:@Athul我非常抱歉。需要使用字符串而不是Guid,而不是添加添加约会的SystemUserID。如何跟踪谁预约了?我不想只为医生创建一个新表,因为我已经在Application Users中保存了所有数据。如果您确信您只需要一个表,那么添加一个UserType来定义ApplicationUser实际上是什么,即医生还是系统用户。我不喜欢那种设计。我觉得如果你和你的实体一起工作,你会做对的。