简体   繁体   中英

The expected type was 'System.Nullable`1[System.Guid]' but the actual value was null

An exception occurred while reading a database value for property 'EMWH.UniqueAttchID'. The expected type was 'System.Nullable`1[System.Guid]' but the actual value was null.

I'm using EFCore 5.0 and I get the error listed above. If in my EMWH view I hide all records where there is a NULL in UniqueAttchID it works fine. But I can't seem to find a way to exclude the records where the principal key (for the relationship) is NULL. But still have the ability to view all records.

Code causing the error

 var workOrder = await _context.EMWHs.AsNoTracking()
                   .Include(x => x.EMWIs).ThenInclude(x => x.HQATs)
                   .Where(x => x.KeyID == WorkOrderKeyId).SingleOrDefault();

EMWH

public class EMWH
    {
        public byte EMCo { get; set; }
        public string WorkOrder { get; set; }
        public string Equipment { get; set; }
        public string? Description { get; set; }
        public Guid? UniqueAttchID { get; set; }
        [Column("udServiceRecordYN")]
        public string? ServiceRecordYN { get; set; }
        public char Complete { get; set; }
        public long KeyID { get; set; }
        [Column("DateSched")]
        [Display(Name = "Scheduled Date")]
        public  DateTime ScheduledDate { get; set; }
        public virtual EMEM EMEM { get; set; }
        public virtual  IEnumerable<EMWI> EMWIs { get; set; }
        public virtual IEnumerable<HQAT> HQATs { get; set; }
    }

HQAT

 public class HQAT
    {
        public byte HQCo { get; set; }
        public string FormName { get; set; }
        public string KeyField { get; set; }
        public string Description { get; set; }
        public string AddedBy { get; set; }
        public DateTime? AddDate { get; set; }
        public string DocName { get; set; }
        public int AttachmentID { get; set; }
        public string TableName { get; set; }
        public Guid? UniqueAttchID { get; set; }
        public string OrigFileName { get; set; }
        public string DocAttchYN { get; set; }
        public string CurrentState { get; set; }
        public int? AttachmentTypeID { get; set; }
        public string IsEmail { get; set; }
        public long KeyID { get; set; }
        public virtual udEMCD EMCD { get; set; }
        public virtual HQAF HQAF { get; set; }
        public virtual EMWH EMWH { get; set; }
        public virtual EMWI EMWI { get; set; }
        public virtual udEMED EMED { get; set; }
    }

DBContext

modelBuilder.Entity<EMWH>().ToTable("EMWH").HasKey(k=>new { k.EMCo, k.WorkOrder });
modelBuilder.Entity<HQAT>().HasOne(x => x.EMWH).WithMany(x => x.HQATs).HasForeignKey(x => x.UniqueAttchID)
            .HasPrincipalKey(x => x.UniqueAttchID);

You have the relationship set up to count on public Guid? UniqueAttchID { get; set; } public Guid? UniqueAttchID { get; set; } public Guid? UniqueAttchID { get; set; } for keys between entities, but you have this set up as a nullable GUID type, so when the query runs the DB is likely coming across a null value in the table, and can't resolve the relationship. The simple solution and arguably best practice is to make those properties non-nullable, or define the relationship using int or long types as ID's, so they can't be null, and making sure your INSERT and UPDATE queries are properly setting the relationships. Either way, the null is where you should start and if you have records in the tables already that have null values you are expecting to use as keys, you could have some work on your hands to figure out how those are supposed to be linked and getting the nulls replaced with GUID values.

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