0

This is the class that I want to connect it to the ApplicationUser class (the default class generated from Identity)

public class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string Name { get; set; }    
    [Required]
    [ForeignKey("ApplicationUser")]
    public string UserId { get; set; }
    public ApplicationUser ApplicationUser { get; set; }
}

This is the ApplicationUser class :

public class ApplicationUser : IdentityUser
{
    public virtual ICollection<Post> UserPosts { get; set; }
    [Required]
    public string UserProfileId { get; set; }
    [ForeignKey("UserProfileId")]
    [Required]
    public virtual UserProfile UserProfile { get; set; }
    //... GenerateUserIdentityAsync()   function
}

This is the DbContext (it's the default one from Identity):

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<Post> Post { get; set; }
    public System.Data.Entity.DbSet<UserProfile> UserProfile { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<ApplicationUser>()
        .HasRequired(b => b.UserProfile)
            .WithRequiredDependent(a => a.ApplicationUser);

        base.OnModelCreating(modelBuilder);
    }
}

I tried multiple variations in OnModelCreating function but with no good result. For the above code this is the error that I am getting when I try to add new migration :

ApplicationUser_UserProfile_Target: : 
Multiplicity is not valid in Role 'ApplicationUser_UserProfile_Target' in relationship        

'ApplicationUser_UserProfile'. Because the Dependent Role properties are not the key properties, 
the upper bound of the multiplicity of the Dependent Role must be '*'.

I am using ASP.NET MVC5 with Entity Framework 6 in VS2015.

I managed to set a one to many relationship between Users table and a custom class but this one I can't find any right solution.

Please send help.

Alin Panainte
  • 101
  • 1
  • 4
  • Common issue - see [here](https://stackoverflow.com/questions/10292355/how-do-i-create-a-real-one-to-one-relationship-in-sql-server). You can't do it that way where both ends are required, so either make one side nullable or add fluent code with `HasOptional`. – Steve Greene Nov 16 '17 at 21:41
  • Why would you even like to do that? It can make sense in an 1 to 1..0 but not a 1 to 1. – bnu Dec 13 '17 at 09:23
  • @bnu I understand that true 1 to 1 is not possible. Eventually I went with 1 to 1...0 and got it working. – Alin Panainte Dec 13 '17 at 21:07
  • Ok, i'm just wondering, what was your reason to cut those models in two and not creating just the application user holding all the properties? – bnu Dec 14 '17 at 12:23
  • 1
    I wanted to have a flexible custom class. With the Application User class is best to use the Identity API to get info. I am at the beginning with the Identity model and I didn't want it to cause me additional difficulties than a classic model. – Alin Panainte Dec 15 '17 at 07:07
  • Ah, I see. Personally I never had any issue when treating Identity Models like the "normal" ones. – bnu Dec 20 '17 at 11:35

0 Answers0