我有一堂课:
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或数据注释。
我该如何指定关系的主要终点?
最佳答案
这里有一个很好的例子说明这种问题:
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; }