Reputation: 42337
I'm using Entity Framework's Fluent API to map entities to a database.
public class Ticket
{
public int Id { get; set; }
public virtual SalesOrder SalesOrder { get; set; }
public int SalesOrderId { get; set; }
public virtual ICollection<TicketLine> Lines { get; set; }
}
public class SalesOrder
{
public int Id { get; set; }
public virtual ICollection<Ticket> Tickets { get; set; }
}
public class TicketLine
{
public int Id { get; set; }
public Ticket Ticket { get; set; }
public int TicketId { get; set; }
}
public class TicketMap : EntityTypeConfiguration<Ticket>
{
public TicketMap() {}
}
public class SalesOrderMap : EntityTypeConfiguration<SalesOrder>
{
public SalesOrderMap()
{
HasMany(t => t.Tickets)
.WithRequired(t => t.SalesOrder)
.HasForeignKey(t => t.SalesOrderId);
}
}
public class TicketLineMap : EntityTypeConfiguration<TicketLine>
{
public TicketLineMap() {}
}
When I run a query related to Ticket
, I receive the following EntityCommandExecutionException
:
Invalid column name 'Ticket_Id'.
Ticket
have their relationships definedUpvotes: 3
Views: 3931
Reputation: 42337
The problem turned out to be that I'd forgotten to tell Entity Framework how to associate Ticket
s with TicketLine
s.
I resolved it by configuring the relationship:
HasMany(t => t.Lines)
.WithRequired(t => t.Ticket)
.HasForeignKey(t => t.TicketId);
Upvotes: 2