【问题标题】:The expected type was 'System.Nullable`1[System.Guid]' but the actual value was null预期的类型是 'System.Nullable`1[System.Guid]' 但实际值​​为 null
【发布时间】:2020-08-19 12:17:18
【问题描述】:

读取属性“EMWH.UniqueAttchID”的数据库值时发生异常。预期的类型是 'System.Nullable`1[System.Guid]' 但实际值​​为 null。

我使用的是 EFCore 5.0,但出现上面列出的错误。如果在我的 EMWH 视图中隐藏 UniqueAttchID 中有 NULL 的所有记录,则它可以正常工作。但我似乎无法找到一种方法来排除主键(用于关系)为 NULL 的记录。但仍然可以查看所有记录。

导致错误的代码

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

总部

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

【问题讨论】:

    标签: c# sql-server linq entity-framework-core


    【解决方案1】:

    您已将关系设置为依赖 public Guid? UniqueAttchID { get; set; } 来获取实体之间的键,但您已将此设置为可为空的 GUID 类型,因此当查询运行时,数据库可能会遇到表中的空值,并且无法解决关系。简单的解决方案和可以说是最佳实践是使这些属性不可为空,或使用intlong 类型作为ID 定义关系,因此它们不能为空,并确保您的INSERT 和@987654325 @ 查询正在正确设置关系。无论哪种方式,null 都是您应该开始的地方,如果表中的记录已经具有您希望用作键的 null 值,您可以做一些工作来弄清楚这些应该如何链接和将空值替换为 GUID 值。

    【讨论】:

    • 感谢您的回复。我无法控制底层数据库,因为它不是我的,它被用于另一个软件。 HQAT 表是一个存储整个系统中所有数据库附件的表,它引用该 GUID。但是,如果没有与 EMWH 表单关联的附件,则记录将其设为空。如果有与之关联的记录,则该记录与 HQAT 记录具有相同的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    相关资源
    最近更新 更多