这是我想将它连接到ApplicationUser类的类(从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; }
}
这是ApplicationUser类:
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
}
这是DbContext(它是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);
}
}
我在OnModelCreating函数中尝试了多种变体,但效果不佳。 对于上面的代码,这是我在尝试添加新迁移时遇到的错误:
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 '*'.
我在VS2015中使用ASP.NET MVC5和Entity Framework 6。
我设法在Users表和自定义类之间设置了一对多的关系,但是这个我无法找到正确的解决方案。
请发送帮助。