c# - 在 Entity FrameWork 中为多种用途映射相同的模型类

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

我有两个模型类,一个是 ApplicationUser,第二个是 Appointment。应用程序用户包括使用该应用程序的所有用户,在我的例子中是医生和数据输入运算符(operator)。每次预约都会分配医生,数据输入运算符(operator)会将此日志记录到数据库中。我想通过预约映射这两个用户。我试过这样的事情

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。因此,要么在您的约会实体中使用 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 classes中Id的类型改为int。可以求助here .

关于c# - 在 Entity FrameWork 中为多种用途映射相同的模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28426452/

相关文章:

c# - 简单的http服务器

php - 如何每分钟自动检查MySQL数据库中的记录..?

asp.net-mvc - 不记名代币和 CSRF

asp.net - 如何强制编译 ASP.NET MVC View ?

c# - C#从文本文件中读取两行数据时出现异常

c# - 在页面加载时停止触发事件

c# - 是否可以在 ASP.NET 中直接写入/读取 TCP 流?

javascript - 使用 Javascript 在 GridView 中设置值文本框

asp.net-mvc - MVC 按钮单击在新窗口中打开

c# - Windows 服务的后台 worker