我试图建立一对一的关系。请问我在哪里写这些专栏?
modelBuilder.Entity<OfficeAssignment>()
.HasRequired(t => t.Instructor)
.WithOptional(t => t.OfficeAssignment);
非常感谢!
答案 0 :(得分:1)
您将在上下文类中的OnModelCreating方法
中编写这些代码行 public class MyContext : DbContext
{
public DbSet<OfficeAssignment> OfficeAssignments { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<OfficeAssignment>()
.HasRequired(t => t.Instructor)
.WithOptional(t => t.OfficeAssignment);
base.OnModelCreating(modelBuilder);
}
}
答案 1 :(得分:1)
您可以在上下文类中编写它,例如:
public class AssignmentContext : DbContext
{
public IDbSet<OfficeAssignment> OfficeAssignments { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<OfficeAssignment>()
.HasRequired(t => t.Instructor)
.WithOptional(t => t.OfficeAssignment);
base.OnModelCreating(modelBuilder);
}
}