[英]Configuring a Relationship Where Both Ends Are Required (One-to-One)
the sample code on http://msdn.microsoft.com/en-US/data/jj591620#RequiredToRequired is it even correct? http://msdn.microsoft.com/zh-CN/data/jj591620#RequiredToRequired上的示例代码是否正确? the code is asking for OfficeAssignment prop on the Instructor class.
代码要求在Instructor类上使用OfficeAssignment道具。 it will not resolve for obvious reasons.
由于明显的原因它不会解决。 what is the correct way of having a one-to-one relationship on ef now?
现在在ef上建立一对一关系的正确方法是什么?
// Configure the primary key for the OfficeAssignment
modelBuilder.Entity<OfficeAssignment>()
.HasKey(t => t.InstructorID);
modelBuilder.Entity<Instructor>()
.HasRequired(t => t.OfficeAssignment)
.WithRequiredPrincipal(t => t.Instructor);
public class OfficeAssignment
{
// Specifying InstructorID as a primary
[Key()]
public Int32 InstructorID { get; set; }
public string Location { get; set; }
// When the Entity Framework sees Timestamp attribute
// it configures ConcurrencyCheck and DatabaseGeneratedPattern=Computed.
[Timestamp]
public Byte[] Timestamp { get; set; }
// Navigation property
public virtual Instructor Instructor { get; set; }
}
public class Instructor
{
public Instructor()
{
this.Courses = new List<Course>();
}
// Primary key
public int InstructorID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public System.DateTime HireDate { get; set; }
// Navigation properties
public virtual ICollection<Course> Courses { get; private set; }
}
There is mistake around the Local/office nav property. 本地/办公室导航属性周围有错误。 A few deliberate renames to clarify Perhaps this....
一些故意的重命名来澄清也许这...
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace one2one
{
class Program
{
static void Main(string[] args)
{
var context = new Demo();
var instructor = new Instructor();
instructor.FirstName = "Big";
instructor.LastName = "Willi";
context.Set<Instructor>().Add(instructor);
var office = new OfficeAssignment();
office.Location = "is this where the demo broke down ? See POCO ";
office.InstructorUsingThisOffice = instructor;
context.Set<OfficeAssignment>().Add(office);
context.SaveChanges();
}
}
public class OfficeAssignment
{
// Specifying InstructorID as a primary
public Int32 InstructorID { get; set; }
public string Location { get; set; }
// Navigation property
public virtual Instructor InstructorUsingThisOffice { get; set; }
}
public class Instructor
{
// Primary key
public int InstructorID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
//navigation
//missing
public virtual OfficeAssignment TheofficeToUse { get; set; }
}
public class Demo : DbContext
{
DbSet<OfficeAssignment> officeAssignments { get; set; }
DbSet<Instructor> Instructors { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure the primary key for the OfficeAssignment
modelBuilder.Entity<OfficeAssignment>()
.HasKey(t => t.InstructorID);
modelBuilder.Entity<Instructor>()
.HasRequired(t => t.TheofficeToUse)
.WithRequiredPrincipal(d => d.InstructorUsingThisOffice); //current entity is principal, the navigation back.
// and we share the same key... MUST with EF 1:1 foreign key
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.