简体   繁体   中英

ArgumentNullException: Value cannot be null. Parameter name: key on Include Statement

Worked a lot with EF 6.x (via designer) and now started on a new project using EF Core.

I'm getting an error that says value cannot be null, not sure exactly what I'm doing wrong. I've got rid of a lot of fields for brevity as there are hundreds.

All these tables are views via synonyms that connect to a different database. I can get it to work fine, if I do each individual call to a database, but as soon as I do include. I get an error on that line. The error I'm getting is

ArgumentNullException: Value cannot be null. Parameter name: 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();

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

}

Context

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

EDIT: I added nullable KeyID's just to test prior to uploading and still didn't work.

I think the error is that you're declaring the Key as nullable, which it should never happen.

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

change your code to this...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM