簡體   English   中英

未加載FK關系,因為該類型不可用

[英]The FK relationship was not loaded because the type is not available

在我們的C#批處理作業之一中,我們使用使用Entity Framewok 4.1構建的dll。 但是,批處理作業也使用EF(相同版本),這看起來可能會引起一些麻煩。

在dll和批處理作業的.edmx文件中, AgentTransmissionAgentRelationshipCodes表之間是一種關系。 AgentTransmission有一個一對多的關系AgentRelationshipCodes AgentTransmission的PK用作AgentRelationshipCodes表上的FK字段。

下面的代碼部分來自dll中的例程。 批處理作業調用此方法以在AgentTransmission表上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第一件事是,該消息將MonetModel引用為FK關系的命名空間。 MonetModel命名空間用於在批處理作業中嚴格使用的EF DbContext DbContextTransmitAgents屬於dll中使用的Botticelli名稱空間。

從下面的屏幕快照中可以看到,但是,在兩個名稱空間中都定義了這種FK關系(一個由批處理作業使用,一個由dll使用)

在此處輸入圖片說明

出於良好的考慮,這里是從dll和批處理作業EF名稱空間中提取的相關類/字段

DLL-代理傳輸

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

發生這種情況的原因是,當我將表添加到.edmx中時,設置了“復數化”選項。 這導致了類名稱,如下所示:

DLL

namespace Botticelli
{
    public partial class AgentRelationshipCodes
    {

批處理作業

namespace LVOAGT
{
    public partial class AgentRelationshipCode
    {

只需使名稱統一並解決問題即可。

當我創建新表“ post”並將外鍵關系與另一個“類別”表關聯后,通過右鍵單擊edmx文件運行自定義工具。 在運行應用程序時,出現以下錯誤。 通過包括與類別類的表外鍵關系來解決該問題。

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

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

我已包含在類別模型class中。有關更多詳細信息,請訪問: http//www.infinetsoft.com/Post/-Solved-The-relationship-model-was-not-loaded-因為-the-type-model-is-not -可選/ 1244

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM