未加载FK关系,因为该类型不可用

时间:2014-06-02 20:25:31

标签: c# entity-framework dll

在我们的一个C#批处理作业中,我们使用了使用Entity Framewok 4.1构建的dll。但是,批处理作业也使用EF(相同版本),看起来这可能会造成一些麻烦。

在dll和批处理作业中,.edmx文件是AgentTransmissionAgentRelationshipCodes表之间的关系。 AgentTransmissionAgentRelationshipCodes有一对多的关系。 AgentTransmission的PK用作AgentRelationshipCodes表上的FK字段。

下面的代码部分来自dll中的例程。批处理作业调用此方法以查找AgentTransmission表上状态为Waiting(或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"));

当上面的最后一行运行时,它会遇到以下异常

Schema specified is not valid. Errors: 

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

我在网上的一些帖子中看到了这条错误消息,但是我们无法找到解决方案。目前我只是在寻找一个搜索方向。

然而,第一件事就是我的消息是引用MonetModel作为FK关系的命名空间。 MonetModel命名空间用于批处理作业中严格使用的EF DbContext。但是,DbContextTransmitAgents属于dll中使用的Botticelli命名空间。

然而,正如您在下面的屏幕截图中看到的,这个FK关系在两个命名空间中定义(一个由批处理作业使用,一个由dll使用)

enter image description here

为了更好地衡量,这里是从dll和批处理作业EF命名空间中提取的相关类/字段

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

批量作业 - 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; }
    }
}

批量作业 - 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; }
    }
}

批处理作业 - 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; }
    }
}

2 个答案:

答案 0 :(得分:1)

这是因为当我将表添加到.edmx时设置了“复数化”选项。这导致类名如下:

<强> DLL

namespace Botticelli
{
    public partial class AgentRelationshipCodes
    {

批量作业

namespace LVOAGT
{
    public partial class AgentRelationshipCode
    {

简单地使名称统一并解决问题。

答案 1 :(得分:1)

当我创建新表“post”并将外键关系提供给另一个“category”表后,通过右键单击edmx文件运行自定义工具。在运行应用程序时,我收到以下错误。它通过将post table外键关系包含在类别类中来解决。

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

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

我已经包含在类别模型类中了解更多详细信息: http://www.infinetsoft.com/Post/-Solved-The-relationship-model-was-not-loaded-because-the-type-model-is-not-available/1244