我试图设置User
表和UserSettings
表之间的一对一关系。HasRequired为什么不能正常工作?
尽管指定了“HasRequired”关系,但我仍然可以在没有任何设置的情况下插入用户。我哪里错了?
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
// Settings = new UserSettings();
}
public string EmailAddress { get; set; }
public virtual UserSettings Settings { get; set; }
}
public class UserSettings
{
[Key]
public string ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().HasRequired(t => t.Settings).WithRequiredPrincipal(t => t.User);
}
}
你在哪里实现了'OnModelCreating()'方法? –
当你说“能够插入”时,你的意思是什么?你的代码是什么,发生了什么,以及你期望发生什么? –
你是否在应用的派生DbContext中重写'OnModelCreating()'?如果没有,你应该在那里做。 –