我在提供的ApplicationUser
框架内扩展了IndentityModel
类,以包含配置文件信息(称为专家)
namespace learn4.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
// so a new Profile is created at the time of registeration
if (expert == null) expert = new Expert();
}
public virtual Expert expert { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<Expert> Experts { get; set; }
}
}
专家定义如下
public class Expert
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; } //
public string FirstName { get; set; } //
public string LastName { get; set; } //
public string MiddleNames { get; set; } //
public DateTime? DateofBirth { get; set; } //
}
我希望,
答案 0 :(得分:1)
首先,首先考虑Expert
是没有意义的。身份的全部意义在于允许您以任何您认为合适的方式定制“用户”等内容。因此,添加其他“配置文件”信息的正确方法是在用户对象上:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; } //
public string LastName { get; set; } //
public string MiddleNames { get; set; } //
public DateTime? DateofBirth { get; set; } //
}
其次,你不是在这里创造一对一,而是一对多。要创建1对1,您需要在Expert
课程中添加以下内容:
public virtual ApplicationUser User { get; set; }
这应该足以让Entity Framework猜测你正在定义一对一,但我总是想具体说明我的意图:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>()
.HasRequired(m => m.Expert)
.WithRequiredPrincipal(m => m.User);
}