ArgumentNullException:值不能为null。参数名称:包含语句上的键

时间:2018-09-06 10:13:07

标签: c# sql-server ef-core-2.1

(通过设计人员)与EF 6.x进行了大量合作,现在开始使用EF Core进行新项目。

我收到一条错误消息,指出value不能为null,不确定我在做什么错。为了简洁起见,我已经摆脱了很多领域。

所有这些表都是通过同义词连接到不同数据库的视图。如果我对数据库进行每个单独的调用,但只要包含,就可以使它正常工作。我在那条线上出现错误。我收到的错误是

ArgumentNullException:值不能为null。 参数名称:键 System.Collections.Generic.Dictionary.FindEntry(TKey键)

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();

EMEM

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

编辑:我添加了可空键ID,只是为了在上传之前进行测试,但仍然无法正常工作。

1 个答案:

答案 0 :(得分:1)

我认为错误是您将Key声明为可为空,这是永远不会发生的。

[Key]
public long? KeyID { get; set; }

将代码更改为此...

[Key]
public long KeyID { get; set; }