Reputation: 51064
I have a class:
public partial class CanteenTerminal : XTimeEntity
{
public virtual Terminal Terminal { get; set; }
public short TerminalId { get; set; }
}
and its mapping class:
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);
}
I also have class 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; }
}
And its mapping file:
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);
}
Whenever the data model is built, i.e. when I run a test query, I get the following error. I get this for several one-to-one relationships, and I have just removed those properties from the entities temporarily just to avoid errors from entities I am not yet interested in.
Unable to determine the principal end of an association between the types 'XTime.Data.CanteenTerminal' and 'XTime.Data.Terminal'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
Just how do I specify the principal end of the relationship?
Upvotes: 2
Views: 303
Reputation: 159
There is a good example of this type of problem here: http://msdn.microsoft.com/en-us/data/jj591620#RequiredToRequired
The mapping for the primary keys here is:
modelBuilder.Entity<OfficeAssignment>()
.HasKey(t => t.InstructorID);
modelBuilder.Entity<Instructor>()
.HasRequired(t => t.OfficeAssignment)
.WithRequiredPrincipal(t => t.Instructor);
I did have to add an additional nav property (in the instructor class) in the sample, it seems to work ok:
// Navigation property
public virtual OfficeAssignment OfficeAssignment { get; set; }
Upvotes: 1
Reputation: 925
As a test, I would suggest something crazy:
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; }
};
Upvotes: 0