The FK relationship was not loaded because the type is not available

c# dll entity-framework

Question

In one of our C# batch jobs, we use a dll that was built using the Entity Framewok 4.1. The batch job, however, also uses the EF (same version) and it looks like this might be causing some trouble.

In both the dll and batch job's .edmx files is a relationship between the AgentTransmission and AgentRelationshipCodes table. AgentTransmission has a one-to-many relationship with AgentRelationshipCodes. The PK of AgentTransmission serves as a FK field on the AgentRelationshipCodes table.

The section of code below is from a routine in the dll. This method is called by the batch job to look for any record on the AgentTransmission table with a status of Waiting (or W).

namespace Botticelli
{
    public class MonetToMainframe : IMonetToMainframe
    {
        private static TransmitAgents _db;

        public ResponseMessage TransmitWaitingAgents()
        {
            try
            {
                _db = new TransmitAgents();

                //Exception thrown here
                agtTran = _db.AgentTransmission.FirstOrDefault(w => w.RecordStatus.Equals("W"));

When the last line above is run it hits the following exception

Schema specified is not valid. Errors: 

The relationship 'MonetModel.FK__AgentRela__AgtTa__3449B6E4' was not loaded because the 
type 'MonetModel.AgentRelationshipCode' is not available.

I've seen this error message in a number of posts on the net but haven't been able to find a solution. At the moment I'm just stuck looking for even a direction to search in.

This first thing that jumps out at me, however, is that the message is referencing MonetModel as the namespace of the FK relationship. The MonetModel namespace is used the the EF DbContext that is used strictly in the batch job. The DbContext class TransmitAgents, however belongs to the Botticelli namespace which is used in the dll.

As you can see from the screen shot below, however, this FK relationship is defined in both namespaces (one used by the batch job, one by the dll)

enter image description here

For good measure here are the relevant classes/fields pulled from both the dll and batch job EF namespaces

DLL - AgentTransmission

namespace Botticelli
{
    using System;
    using System.Collections.Generic;

    public partial class AgentTransmission
    {
        public AgentTransmission()
        {
            this.AgentRelationshipCodes = new HashSet<AgentRelationshipCodes>();
        }

        //Tons of fields here, edited for readability

        public virtual ICollection<AgentRelationshipCodes> AgentRelationshipCodes { get; set; }
    }
}

DLL - AgentRelationshipCodes

namespace Botticelli
{
    using System;
    using System.Collections.Generic;

    public partial class AgentRelationshipCodes
    {
        public System.Guid Id { get; set; }
        public string RelationshipId { get; set; }
        public Nullable<System.DateTime> EffectiveDate { get; set; }
        public System.DateTime LastChangeDate { get; set; }
        public string LastChangeId { get; set; }
        public Nullable<int> AgtTableId { get; set; }

        public virtual AgentTransmission AgentTransmission { get; set; }
    }
}

DLL - DbContext

namespace Botticelli
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class TransmitAgents : DbContext
    {
        public TransmitAgents()
            : base("name=TransmitAgents")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<TransmissionHistory> TransmissionHistory { get; set; }
        public DbSet<AgentRelationshipCodes> AgentRelationshipCodes { get; set; }
        public DbSet<AgentTransmission> AgentTransmission { get; set; }
    }
}

Batch Job - AgentTransmission

namespace LVOAGT
{
    using System;
    using System.Collections.Generic;

    public partial class AgentTransmission
    {
        public AgentTransmission()
        {
            this.AgentRelationshipCodes = new HashSet<AgentRelationshipCode>();
        }

        //Tons of fields here, edited for readability

        public virtual ICollection<AgentRelationshipCode> AgentRelationshipCodes { get; set; }
    }
}

Batch Job - AgentRelationshipCodes

namespace LVOAGT
{
    using System;
    using System.Collections.Generic;

    public partial class AgentRelationshipCode
    {
        public System.Guid Id { get; set; }
        public string RelationshipId { get; set; }
        public Nullable<System.DateTime> EffectiveDate { get; set; }
        public System.DateTime LastChangeDate { get; set; }
        public string LastChangeId { get; set; }
        public Nullable<int> AgtTableId { get; set; }

        public virtual AgentTransmission AgentTransmission { get; set; }
    }
}

Batch Job - DbContext

namespace LVOAGT
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class MonetDb : DbContext
    {
        public MonetDb()
            : base("name=MonetDb")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<BatchDashboard> BatchDashboard { get; set; }
        public DbSet<TransmissionHistory> TransmissionHistories { get; set; }
        public DbSet<ConfigStuff> ConfigStuffs { get; set; }
        public DbSet<AgentRelationshipCode> AgentRelationshipCodes { get; set; }
        public DbSet<AgentTransmission> AgentTransmissions { get; set; }
    }
}
1
1
6/2/2014 8:33:22 PM

Accepted Answer

When I created new table “post” and gave foreign key relation to another “category” table after that run the custom tool by right click on edmx file. While running the application I got the following error. It was resolved by including post table foreign key relation to the category class.

 public Category()
    {
        this.Posts = new HashSet<Post>();

    }
 public virtual ICollection<Post> Posts { get; set; }

I have included in category model class.for more details: http://www.infinetsoft.com/Post/-Solved-The-relationship-model-was-not-loaded-because-the-type-model-is-not-available/1244

1
5/25/2016 2:29:27 PM

Popular Answer

This was occuring because the "pluralization" option was set when I added the tables to the .edmx. This resulted in Class names like so:

DLL

namespace Botticelli
{
    public partial class AgentRelationshipCodes
    {

Batch Job

namespace LVOAGT
{
    public partial class AgentRelationshipCode
    {

Simply made the names uniform and problem solved.



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