1

I'm having problems trying to create relationships of 0..1 to 0..1 using code first between AspNetUsers table extending the applicationuser class and MyUserInfo class. The nature of the relationship is that a user can have one MyUserInfo or not.

My Models:

 public class MyUserInfo
{
    [Key]
    public int MyUserInfoId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual ApplicationUser User { get; set; }
}

ApplicationUser class:

public class ApplicationUser : IdentityUser
{
    public int? MyUserInfoId { get; set; }

    [ForeignKey("MyUserInfoId")]
    public virtual MyUserInfo MyUserInfo { get; set; }
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

Logically it should work. Got two foreign keys. One foreign key in ApplicationUser class (AspNetUser table) and one foreign key in MyUserInfo class.

Somehow I get this error on adding migration:

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

Why do I get this kind of error?

Thanks in advance.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
Kim Thannh Ly
  • 21
  • 1
  • 3
  • possible duplicate of [What does principal end of an association means in 1:1 relationship in Entity framework](http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr) – trailmax Jul 29 '15 at 09:34

0 Answers0