3

Background

I'm using Entity Framework's Fluent API to map entities to a database.

Entities

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; }
}

Mapping

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() {}
}

Problem

When I run a query related to Ticket, I receive the following EntityCommandExecutionException:

Invalid column name 'Ticket_Id'.

What I've Tried

  1. Making sure that all entities that reference Ticket have their relationships defined
  2. Making sure there aren't any duplicate navigation properties
  3. Checking other answers for this exception on this site

1 Answer 1

2

The problem turned out to be that I'd forgotten to tell Entity Framework how to associate Tickets with TicketLines.

I resolved it by configuring the relationship:

HasMany(t => t.Lines)
    .WithRequired(t => t.Ticket)
    .HasForeignKey(t => t.TicketId);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.