【问题标题】:Map same Model class for multiple purposes in Entity FrameWork在 Entity FrameWork 中为多种目的映射同一个模型类
【发布时间】:2015-02-10 07:41:54
【问题描述】:

我有两个模型类,一个是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: : 引用约束的Dependent Role 中所有属性的类型必须与Principal Role 中对应的属性类型相同。实体“Appointment”上的属性“DoctorID”类型与引用约束“Appointment_Doctor”中实体“ApplicationUser”上的属性“Id”类型不匹配。

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

【问题讨论】:

    标签: c# asp.net asp.net-mvc entity-framework


    【解决方案1】:

    IdentityUser 作为 asp.net 身份实体框架中的所有实体都以 string 为键。您正在尝试映射到 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; }
    }
    

    或将身份类中的 Id 类型更改为 int。你可以找到帮助here

    【讨论】:

    • 现在错误变成了这个。 Appointment_Doctor_Target_Appointment_Doctor_Source: : 一个参照约束的Dependent Role中所有属性的类型必须与Principal Role中对应的属性类型相同。实体“Appointment”上的属性“DoctorID”类型与引用约束“Appointment_Doctor”中实体“ApplicationUser”上的属性“Id”类型不匹配。 Appointment_SystemUser_Target_Appointment_SystemUser_Source:
    • ..... Appointment_Doctor_Target_Appointment_Doctor_Source: : 一个参照约束的Dependent Role中所有属性的类型必须与Principal Role中对应的属性类型相同。实体“Appointment”上的属性“DoctorID”类型与引用约束“Appointment_Doctor”中实体“ApplicationUser”上的属性“Id”类型不匹配。 Appointment_SystemUser_Target_Appointment_SystemUser_Source:
    • @Athul 非常抱歉。需要使用字符串而不是 Guid。
    【解决方案2】:

    您的课程中有多个问题。

    什么是DoctorID?它是在哪里定义的?

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

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

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

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

    SystemUser 添加一个约会,因此该表应包含与该约会相关的数据,即医生 ID 和约会 ID。

    更新:

    根据您的评论,您可以执行以下操作。请注意仅供参考,您更适合定义更好的 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
    }
    

    【讨论】:

    • 而不是添加添加约会的 SystemUserID。我如何追踪预约的人?
    • 我不想只为医生创建一个新表,因为我已经将所有数据保存在应用程序用户中
    • 如果您确信您只需要单个表,那么添加一个 UserType 来定义 ApplicationUser 的实际含义,即 Doctor 或 SystemUser。我不喜欢那个设计。我觉得如果你与你的实体一起工作,你会做对的。
    【解决方案3】:

    更复杂的错误:

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

    每个表都有 3 - 7 个字段的复合键, 并且一个表引用了自己的 3 字段键,其中包含不同的列组合。

    我多年来一直在努力修复一系列字段(它确实修复了其他帖子中提到的错误),只是为了在其他实体中产生连锁反应。

    解决办法: 按照减少出现的顺序排列所有链接表的外键字段

    原来:

    对齐关键字段后:

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

    这解决了所有的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 2011-04-01
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多