不一致的 null 赋值

Inconsistent null assignment

提问人:decoder 提问时间:10/21/2019 最后编辑:decoder 更新时间:10/21/2019 访问量:61

问:

无法为以下代码赋值 null 值

    [Table("POSMachine")]
    public class POSMachine
    {
        public int ID { get; set; }
        public string POSSN { get; set; }
        public int? ReceivedBy { get; set; }
        public int? ReceivedFrom { get; set; }
        [ForeignKey("ReceivedFrom")]
        public virtual TeamMember ReceivedFromMember { get; set; }
        [ForeignKey("ReceivedBy")]
        public virtual TeamMember ReceivedByMember { get; set; }
    }

    private void LoadMachineObj(POSMachine machine)
    {
        var receivebymember = _context.TeamMembers.Where(m => m.IsActive == true && m.ID == receivebyid).FirstOrDefault();
            machine.ReceivedByMember = receivebymember;
        var receivefrommember = _context.TeamMembers.Where(m => m.IsActive == true && m.ID == 1).FirstOrDefault();
        machine.ReceivedFromMember = receivefrommember;
    }

//calling method:
    var machine = _context.POSMachines.Where(v => v.ID == id).FirstOrDefault();
    LoadMachineObj(machine);
    _context.Entry(machine).State = EntityState.Modified;
    _context.SaveChanges();

如果变量为 null,则保存上一个对象引用,类似于 。但有时会变成空。如果为 null,为什么有时变为 null,有时不变为 null。我的实体对象定义是否正确?receivefrommembermachine.ReceivedFromMemberSystem.Data.Entity.DynamicProxies.TeamMember_98A56E4F3F20CCE075A377A6C5A2A9E1C6BFE2A0F00566726C986B9313FBB82Creceivefrommembermachine.ReceivedFromMember

C# 实体框架 按值传递

评论

2赞 Pavel Anikhouski 10/21/2019
ReceivedFromMember是虚拟财产,也许它在某个地方被覆盖了?
0赞 Fabio 10/21/2019
你是如何使用这种方法的?

答: 暂无答案