首页 文章

ASP.NET MVC:如何正确引用自定义对象到ApplicationUser?

提问于
浏览
1

目前我正在尝试将我的自定义对象连接到由Visual Studio自动生成的Application User .

Employee.cs

public class Employee
{
    [Display(Name = "ID")]
    [Key]
    public int EmployeeID { get; set; }

    [ForeignKey("ApplicationUser")]
    public int ApplicationUserID { get; set; }

    [Required]
    [DataType(DataType.Currency)]
    public decimal MonthlySalary { get; set; }

    [Required]
    public string Experience { get; set; }

    [Required]
    public string Description { get; set; }

    public string PhotoURL { get; set; }

    public virtual ApplicationUser ApplicationUser { get; set; }
}

错误是:

运行所选代码生成器时出错:'无法检索Models.Tables.Employee的元数据 . 在模型生成期间检测到一个或多个验证错误:Parent_ApplicationUser_Target_Parent_ApplicationUser_Source ::参照约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同 . 实体“Parent”上的属性“ApplicationUserlD”的类型与引用约束“Parent_ApplicationUser”中实体“ApplicationUser”上的属性“Id”的类型不匹配 . Employee ApplicationUser目标Employee ApplicationUser Source ::引用约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同 . 实体'Employee上的属性'ApplicationUserlD'的类型与引用约束'Employee_ApplicationUser中的实体'ApplicationUser'上的属性'Id'的类型不匹配 .

Parent 表包含与 Employee 相同的 ApplicationUser 引用 . 我究竟做错了什么?感谢帮助

1 回答

  • 5

    默认情况下(使用CodeFirst创建新项目时由VS生成), ApplicationUser 的Id为 string 类型 . EmployeeApplicationUser 中的外键类型为 int ,因此类型不匹配 .

    public class ApplicationUser : IdentityUser
    {
      //Id in IdentityUser is of type string
    }
    

相关问题