简体   繁体   中英

ASP.NET MVC: How to properly reference custom object to ApplicationUser?

currently I am trying to connect my custom object to Application User, which is auto-generated by Visual Studio.

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; }
}

Error is:

There was an error running the selected code generator: 'Unable to retrieve metadata for Models.Tables.Employee. One or more validation errors were detected during model generation: Parent_ApplicationUser_Target_Parent_ApplicationUser_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 'ApplicationUserlD' on entity 'Parent' does not match the type of property 'Id' on entity 'ApplicationUser' in the referential constraint 'Parent_ApplicationUser. Employee ApplicationUser Target Employee ApplicationUser 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 'ApplicationUserlD' on entity 'Employee does not match the type of property 'Id' on entity 'ApplicationUser' in the referential constraint 'Employee_ApplicationUser.

Parent table contains same ApplicationUser reference as Employee . What am I doing wrong? Thanks for help

By default (generated by VS while creating new project with CodeFirst), ApplicationUser has Id of type string . Your foreign key in Employee to ApplicationUser is of type int , so there is type mismatch.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM