Search code examples
c#asp.net-mvcentity-frameworkforeign-key-relationship

How to properly add a foreign key from my own model to the EF Userprofile?


my model classes first:

public class Person
{
    [Required, Key]
    public int ID { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string LearntSkillsAndLevelOfSkills { get; set; }
    public string ProfileImage { get; set; }
    public string City { get; set; }
    public string PhoneNr { get; set; }
    [Required]
    public string Email { get; set; }
    public string Hobbys { get; set; }
    public string SkillsToLearn { get; set; }
    public string Stand { get; set; }
    public int YearsOfWorkExperience { get; set; }
    public string HobbyProjectICTRelated { get; set; }
    public string ExtraInfo { get; set; }
    public string Summary { get; set; }

    public int UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual UserProfile profile { get; set; }

}


 [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }

        public Nullable<int> ID { get; set; }
        public virtual Person personprofile { get; set; }

    }

when i run this however it gives me this exception: The principal end of this association must be explicitly configured

i've searched this error but it doesn't clarify it for me... so i absolutely have no clue how to fix this. Basically i want to link my Person class to the Userprofiles so that i can create a login mechanism that automatically lets 1 person who makes an account on the site get 1 Profile to Edit to his own information. He's however not allowed to modify other people their accounts.

I hope this makes my problem clear and that somebody can help me :). i'm using EF 6 btw and i get the error in the class InitializeSimpleMembershipAttribute that comes standard with the MVC example of ASP.net

Greetings and thanks in advance,
Marijn


Solution

  • I managed to get it working with these codes in the models:

    [Table("UserProfile")]
    public class UserProfile
    {
         [Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
         public int UserId { get; set; }
         public string UserName { get; set; }
    
         public virtual Person personprofile { get; set; }
    }
    


    public class Person
    {
        [ForeignKey("profile"), Key]
        public int UserId { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string LearntSkillsAndLevelOfSkills { get; set; }
        public string ProfileImage { get; set; }
        public string City { get; set; }
        public string PhoneNr { get; set; }
        [Required]
        public string Email { get; set; }
        public string Hobbys { get; set; }
        public string SkillsToLearn { get; set; }
        public string Stand { get; set; }
        public int YearsOfWorkExperience { get; set; }
        public string HobbyProjectICTRelated { get; set; }
        public string ExtraInfo { get; set; }
        public string Summary { get; set; }
    
        public UserProfile profile { get; set; }
    }