C# 未加载FK关系,因为类型不可用
在我们的一个C#批处理作业中,我们使用了一个使用实体Framewok 4.1构建的dll。然而,批处理作业也使用EF(相同版本),看起来这可能会引起一些问题 在dll和批处理作业的C# 未加载FK关系,因为类型不可用,c#,entity-framework,dll,C#,Entity Framework,Dll,在我们的一个C#批处理作业中,我们使用了一个使用实体Framewok 4.1构建的dll。然而,批处理作业也使用EF(相同版本),看起来这可能会引起一些问题 在dll和批处理作业的.edmx文件中,是AgentTransmission和AgentRelationshipCodes表之间的关系AgentTransmission与AgentRelationshipCodes具有一对多关系。AgentTransmission的主键用作AgentRelationshipCodes表上的FK字段 下面的代
.edmx
文件中,是AgentTransmission
和AgentRelationshipCodes
表之间的关系AgentTransmission
与AgentRelationshipCodes
具有一对多关系。AgentTransmission
的主键用作AgentRelationshipCodes
表上的FK字段
下面的代码部分来自dll中的一个例程。批处理作业调用此方法以查找AgentTransmission
表中状态为Waiting
(或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
命名空间用于严格在批处理作业中使用的EFDbContext
。然而,DbContext
类TransmitAgents
属于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; }
}
}
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; }
}
}
namespace-Botticelli
{
使用制度;
使用System.Collections.Generic;
公共部分类代理传输
{
公共代理传输()
{
this.AgentRelationshipCodes=新HashSet();
}
//这里有大量字段,为可读性而编辑
公共虚拟ICollection AgentRelationshipCodes{get;set;}
}
}
DLL-代理关系shipCodes
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; }
}
}
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; }
}
}
namespace-Botticelli
{
使用制度;
使用System.Collections.Generic;
公共部分类代理关系装运代码
{
public System.Guid Id{get;set;}
公共字符串RelationshipId{get;set;}
公共可空的EffectiveDate{get;set;}
public System.DateTime LastChangeDate{get;set;}
公共字符串LastChangeId{get;set;}
公共可为空的AgtTableId{get;set;}
公共虚拟代理传输代理传输{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; }
}
}
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; }
}
}
namespace-Botticelli
{
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
公共部分类传输:DbContext
{
公共交通工具()
:base(“名称=传输”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
抛出新代码FirstException();
}
公共数据库集传输历史{get;set;}
公共数据库集代理关系shipCodes{get;set;}
公共数据库集代理传输{get;set;}
}
}
批量作业-代理传输
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; }
}
}
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; }
}
}
名称空间LVOAGT
{
使用制度;
使用System.Collections.Generic;
公共部分类代理传输
{
公共代理传输()
{
this.AgentRelationshipCodes=新HashSet();
}
//这里有大量字段,为可读性而编辑
公共虚拟ICollection AgentRelationshipCodes{get;set;}
}
}
批处理作业-代理关系shipCodes
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; }
}
}
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; }
}
}
名称空间LVOAGT
{
使用制度;
使用System.Collections.Generic;
公共部分类代理关系shipCode
{
public System.Guid Id{get;set;}
公共字符串RelationshipId{get;set;}
公共可空的EffectiveDate{get;set;}
public System.DateTime LastChangeDate{get;set;}
公共字符串LastChangeId{get;set;}
公共可为空的AgtTableId{get;set;}
公共虚拟代理传输代理传输{get;set;}
}
}
批处理作业-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; }
}
}
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; }
}
}
名称空间LVOAGT
{
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
公共部分类MonetDb:DbContext
{
公共货币
:base(“name=MonetDb”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
抛出新代码FirstException();
}
公共DbSet BatchDashboard{get;set;}
公共数据库集传输历史记录{get;set;}
公共数据库集配置文件{get;set;}
公共数据库集代理关系shipCodes{get;set;}
公共数据库集代理传输{get;set;}
}
}
之所以发生这种情况,是因为在我将表格添加到.edmx时设置了“多元化”选项。这导致类名如下:
DLL
namespace Botticelli
{
public partial class AgentRelationshipCodes
{
批量作业
namespace LVOAGT
{
public partial class AgentRelationshipCode
{
简单地统一名称,解决问题 当我创建新表“post”并将外键关系赋予另一个“category”表后,右键单击edmx文件运行自定义工具。在运行应用程序时,我遇到了以下错误。通过将post表外键关系包含到category类中来解决该问题
public Category()
{
this.Posts = new HashSet<Post>();
}
public virtual ICollection<Post> Posts { get; set; }
公共类别()
{
this.Posts=newhashset();
}
公共虚拟ICollection Posts{get;set;}
我已经包括在categ中了