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.