Joshua Kemmerer
Joshua Kemmerer

Reputation: 1673

How can I create a foreign key to an Application User (User?, AspNetUser?) in Code First MVC application?

I feel like an idiot and I have Googled all over, but I can't find an answer for this. How do I create a foreign key to an Application User's Id? I have a class:

public class Enrollment
{
    public int Id { get; set; }

    [Required]
    public Guid UserId { get; set; }
    [ForeignKey("ApplicationUserId")]
    public ApplicationUser User { get; set; }

    [Required]
    [ForeignKey("Major")]
    public int MajorId { get; set; }
    [ForeignKey("MajorId")]
    public Major Major { get; set; }

    [Required]
    [ForeignKey("Term")]
    public int TermId { get; set; }
    [ForeignKey("TermId")]
    public Term Term { get; set; }
}

My error is:

Unable to determine the principal end of an association between the types 'Plasty.Models.Enrollment' and 'Plasty.Models.ApplicationUser'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

I have tried just about everything under the sun. At first, I wanted to have the ApplicationUser to have another property, a foreign key to Enrollment Id, but that wasn't working, so now I'm just putting the user's Id in the Enrollment table.

Here are the two properties I added to my Application User:

 public virtual Enrollment EnrollmentId { get; set; }
 public virtual List<CourseTaken> CoursesTaken { get; set; }

Upvotes: 0

Views: 2832

Answers (3)

Joe the Coder
Joe the Coder

Reputation: 1825

You have:

public Guid UserId { get; set; }
[ForeignKey("ApplicationUserId")]
public ApplicationUser User { get; set; }

You're specifying ApplicationUserId as the foreign key, but the name of the foreign key property on Enrollment is UserId. Make sure the names match. :-)

Upvotes: 0

Martin Noreke
Martin Noreke

Reputation: 4136

Try the following to see if it helps. Reasons are in comments within the class. Per Pengivan's response, "virtual" added where needed as well.

public class Enrollment
{
    public int Id { get; set; }

    [Required]
    // The default column is a string, although it stores a Guid
    public string UserId { get; set; } 
    // If specified, should refer to the field name on this side of the relationship
    // As Pengivan stated, not needed unless there are multiple references.
    [ForeignKey("UserId")] 
    public virtual ApplicationUser User { get; set; }

    [Required]
    public int MajorId { get; set; }
    public virtual Major Major { get; set; }

    [Required]
    public int TermId { get; set; }
    public virtual Term Term { get; set; }
}

Upvotes: 0

Ivan Peng
Ivan Peng

Reputation: 609

You're close. You're missing a virtual declaration on the user parameter.

public class Enrollment {
    //... other get/set methods
    public virtual ApplicationUser User {get; set;}
}

Specifying the ForeignKey name is only required if you have multiple users of the same type.

Upvotes: 2

Related Questions