Schema specified is not valid. Errors: The relationship 'EntityA_EntityBs' was not loaded because the type 'EntityB' is not available

.net entity-framework entity-framework-6

Question

Background

I'm working on a program that allows a third-party program to query a database using Entity Framework. There are several tables in the database, but no foreign keys. I used Entity Framework Fluent API to map the pertinent tables to entities.

Entities

namespace App.Entities
{
    public class Ticket
    {
        public int Id { get; set; }

        public virtual SalesOrder SalesOrder { get; set; }
        public int SalesOrderId { get; set; }
    }

    public class SalesOrder
    {
        public int Id { get; set; }

        public virtual ICollection<Ticket> Tickets { get; set; }
        public virtual ICollection<Ticket> Lines { get; set; }
    }
}

Mappings

The column and table name mappings are missing since, in my opinion, they are not important.

namespace App.Mappings
{
    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);

            HasMany(t => t.Lines)
                .WithRequired(t => t.SalesOrder)
                .HasForeignKey(t => t.SalesOrderId);
        }
    }
}

Entity Framework has appropriately registered the entities and their mappings.

Problem

The following is what I'm gettingMetadataException when I attempt to do a search:

Schema specified is not valid. Errors: The relationship 'App.SalesOrder_Tickets' was not loaded because the type 'App.Ticket' is not available.

Keep in mind that the namespace ofTicket the message above is incorrect. Actually, the entity class is inApp.Entities.Ticket . I'm not sure, though, if this is connected to the issue or not.

Attempts I've Made

  1. Verifying that the relationship looks to be set up properly
  2. checking the model for duplicate classes as recommended in this statement
  3. Verifying the entities' alignment with the namespace recommended in this response
1
4
5/23/2017 12:25:40 PM

Accepted Answer

This set of lines presented a challenge:

public virtual ICollection<Ticket> Tickets { get; set; }
public virtual ICollection<Ticket> Lines { get; set; }

The second collection's element type was intended to be distinct from the first's:

public virtual ICollection<TicketLine> Lines { get; set; }

This change resolved the issue.

4
1/15/2014 3:30:17 AM

Popular Answer

I experienced the similar error, and my issue was that I had Entities with the same name in two separate edmx models. I suppose EF was sending a message because some of the associated entities couldn't be loaded.

I found the solution by deleting entities from one model (I suppose changing the name might also work) and reconstructing the solution.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow