c# - ArgumentNullException:值不能为空。参数名称:Include 语句上的键
问题描述
使用 EF 6.x(通过设计器)进行了大量工作,现在开始使用 EF Core 进行新项目。
我收到一个错误,说 value 不能为 null,不确定我做错了什么。为简洁起见,我已经删除了很多字段,因为有数百个。
所有这些表都是通过同义词连接到不同数据库的视图。如果我对数据库进行每个单独的调用,我可以让它正常工作,但只要我包含。我在那条线上得到一个错误。我得到的错误是
ArgumentNullException:值不能为空。参数名称:key System.Collections.Generic.Dictionary.FindEntry(TKey key)
OnGetAsync
var equipment = _context.EMEMs.Include(x => x.EMEDs).Where(x => x.KeyID.ToString() == key);
EMEM = await equipment.Include(x => x.EMCM).ThenInclude(x=>x.EMCDs).FirstOrDefaultAsync();
记忆体
public class EMEM
{
public byte? EMCo { get; set; }
[Display(Name = "Equipment Code")]
public string Equipment { get; set; }
public string Type { get; set; }
public string Category { get; set; }
public Guid? UniqueAttchID { get; set; }
[Key]
public long KeyID { get; set; }
[NotMapped] public string EquipmentDetails => $"{Equipment.Trim()} - {Description} - {VINNumber}";
public virtual IEnumerable<EMWH> EMWHs { get; set; }
public virtual EMCM EMCM { get; set; }
public virtual IEnumerable<udEMED> EMEDs { get; set; }
}
EMCM
public class EMCM
{
[Key]
public long KeyID { get; set; }
public byte? EMCo { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public string Notes { get; set; }
public virtual IEnumerable<EMEM> EMEMs { get; set; }
public virtual IEnumerable<udEMCD> EMCDs { get; set; }
}
udEMCD
public class udEMCD
{
[Key]
public long KeyID { get; set; }
public byte? Co { get; set; }
public string Category { get; set; }
public string DocumentCategory { get; set; }
public int Seq { get; set; }
public Guid? UniqueAttchID { get; set; }
public virtual udEMDC EMDC { get; set; }
public virtual EMCM EMCM { get; set; }
public virtual IEnumerable<HQAT> HQATs { get; set; }
}
语境
modelBuilder.Entity<EMEM>().ToTable("EMEM").HasOne(x => x.EMCM).WithMany(x => x.EMEMs).HasForeignKey(x => new { x.EMCo, x.Category }).HasPrincipalKey(x => new { x.EMCo, x.Category });
modelBuilder.Entity<EMEM>().ToTable("EMEM").HasMany(x => x.EMEDs).WithOne(x => x.EMEM).HasForeignKey(x => new { x.Co, x.Equipment }).HasPrincipalKey(x => new { x.EMCo, x.Equipment });
modelBuilder.Entity<EMCM>().ToTable("EMCM").HasMany(x => x.EMCDs).WithOne(x => x.EMCM)
.HasForeignKey(x => new { x.Co, x.Category }).HasPrincipalKey(x => new { x.EMCo, x.Category });
modelBuilder.Entity<udEMCD>().ToTable("udEMCD").HasOne(x => x.EMDC).WithMany(x => x.EMCDs)
.HasForeignKey(x => x.DocumentCategory).HasPrincipalKey(x => x.Category);
modelBuilder.Entity<udEMDC>().ToTable("udEMDC").HasMany(x => x.EMEDs).WithOne(x => x.EMDC).HasForeignKey(x => new{ x.DocumentCategory}).HasPrincipalKey(x => new{ x.Category});
modelBuilder.Entity<udEMED>().ToTable("udEMED");
modelBuilder.Entity<EMWH>().ToTable("EMWH");
modelBuilder.Entity<EMWI>().ToTable("EMWI");
modelBuilder.Entity<HQAT>().HasOne(x => x.EMWH).WithMany(x => x.HQATs).HasForeignKey(x => x.UniqueAttchID)
.HasPrincipalKey(x => x.UniqueAttchID);
modelBuilder.Entity<EMWH>().HasOne(x => x.EMEM).WithMany(x => x.EMWHs)
.HasForeignKey(x => new {x.EMCo, x.Equipment}).HasPrincipalKey(x => new {x.EMCo, x.Equipment});
编辑:我添加了可为空的 KeyID 只是为了在上传之前进行测试,但仍然无法正常工作。
解决方案
我认为错误在于您将 Key 声明为可为空的,这永远不会发生。
[Key]
public long? KeyID { get; set; }
将您的代码更改为此...
[Key]
public long KeyID { get; set; }
推荐阅读
- c# - 成功消息后如何重定向登录页面
- byte-buddy - 有什么办法可以让byte buddy生成的这个类文件包含LocalVariableTable?
- loopbackjs - Loopback 4:如何从 loopback 4 访问名称中带有下划线的表?
- python - 如何使用列表推导将函数列表应用于参数列表
- sapui5 - SAP UI5项目中.che文件夹有什么用?
- python-3.x - ( AttributeError: module 'tensorflow_core._api.v2.config' has no attribute 'get_visible_devices' )发生在 Heroku
- sql - 在报表生成器的多个列中拆分 LookupSet 中的多个值
- angular - 如何使用角度剑道网格对日期列进行内联编辑
- r - R Studio 不刷新源文件
- javascript - 循环上的 Vuetify 对话框组件以确认删除事件