我正在使用Entity Framework的Fluent API将实体映射到数据库。
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() {}
}
当我运行与Ticket
相关的查询时,我会收到以下EntityCommandExecutionException
:
列名称'Ticket_Id'无效。
Ticket
的所有实体都已定义其关系答案 0 :(得分:1)
问题原来是我忘了告诉实体框架如何将Ticket
与TicketLine
联系起来。
我通过配置关系来解决它:
HasMany(t => t.Lines)
.WithRequired(t => t.Ticket)
.HasForeignKey(t => t.TicketId);