如何在代码优先映射中指定1-1关系的主端

时间:2014-01-15 06:09:37

标签: c# entity-framework ef-code-first entity-framework-6

我有一个班级:

public partial class CanteenTerminal : XTimeEntity
{
  public virtual Terminal Terminal { get; set; }
  public short TerminalId { get; set; }
}

及其映射类:

public CanteenTerminalMap()
{
  // Primary Key
  HasKey(t => t.Id);

  // Table & Column Mappings
  ToTable("CANTEENTERM");
  Property(t => t.Id).HasColumnName("TERM_CODEID");

  // Relationships
  HasRequired(t => t.Terminal)
    .WithOptional(t => t.CanteenTerminal);
}

我也有班级Terminal

public partial class Terminal : XTimeEntity
{
  public Terminal()
  {
    ControllerInterfacePointers = new List<ControllerInterfacePointer>();
    TerminalParameters = new List<TerminalParameter>();
  }

  public string Name { get; set; }
  public string Version { get; set; }
  public short Enabled { get; set; }
  public virtual CanteenTerminal CanteenTerminal { get; set; }
  public short CanteenTerminalId { get; set; }
  public virtual ICollection<ControllerInterfacePointer> ControllerInterfacePointers { get; set; }
  public virtual ICollection<TerminalParameter> TerminalParameters { get; set; }
}

及其映射文件:

public TerminalMap()
{
  // Primary Key
  HasKey(t => t.Id);

  Property(t => t.Name)
      .IsRequired()
      .HasMaxLength(30);
  Property(t => t.Version)
      .HasMaxLength(8);

  // Table & Column Mappings
  ToTable("TERMINAL");
  Property(t => t.Id).HasColumnName("TERM_CODEID");
  Property(t => t.Name).HasColumnName("TERM_NAME");
  Property(t => t.Version).HasColumnName("TERM_VERSION");
  Property(t => t.Enabled).HasColumnName("TERM_ENABLED");
  Ignore(t => t.MasterId);
  Ignore(t => t.IsActive);

  HasOptional(t => t.CanteenTerminal)
    .WithRequired(t => t.Terminal);
}

每当构建数据模型时,即当我运行测试查询时,我都会收到以下错误。我得到了几个一对一的关系,我刚刚从实体中删除了这些属性,只是为了避免我不感兴趣的实体出错。

  

无法确定之间关联的主要结束   类型'XTime.Data.CanteenTerminal'和'XTime.Data.Terminal'。该   必须使用显式配置此关联的主要结尾   关系流畅的API或数据注释。

我该如何指定关系的主要目标?

2 个答案:

答案 0 :(得分:1)

这里有一个很好的例子: http://msdn.microsoft.com/en-us/data/jj591620#RequiredToRequired

这里主键的映射是:

modelBuilder.Entity<OfficeAssignment>()
    .HasKey(t => t.InstructorID);

modelBuilder.Entity<Instructor>()
    .HasRequired(t => t.OfficeAssignment)
    .WithRequiredPrincipal(t => t.Instructor);

我确实必须在示例中添加一个额外的nav属性(在讲师类中),它似乎工作正常:

// Navigation property 
public virtual OfficeAssignment OfficeAssignment { get; set; }

答案 1 :(得分:0)

作为测试,我会建议一些疯狂的事情:

public partial class Terminal : XTimeEntity
{
  public Terminal()
  {
    ControllerInterfacePointers = new List<ControllerInterfacePointer>();
    TerminalParameters = new List<TerminalParameter>();
  }

  public string Name { get; set; }
  public string Version { get; set; }
  public short Enabled { get; set; }
  [Required] //I think this line will trick EF into liking you.
  public virtual CanteenTerminal CanteenTerminal { get; set; }
  public short CanteenTerminalId { get; set; }
  public virtual ICollection<ControllerInterfacePointer> ControllerInterfacePointers { get; set; }
  public virtual ICollection<TerminalParameter> TerminalParameters { get; set; }
};