Entity Framework 实体的核心集 null 属性

Entity Framework Core Set null one property of entity

我需要保存一个实现 baseEntity 的实体

如果我修改 IdStatus,efcore 会正确更新它,但如果 idStatus 值在其他道具更新时没有改变 => 由于某种原因 efcore 将其设置为 null

idStatus = 20 => 尝试更新到 12 => 成功

idStatus = 12 => 尝试更新描述 => idStatus = null, desc ok

public void Update(TTableEntity updated, int userid)
    {
        toSave = GetById(updated.Idnrr);

        PropertyCopier<TTableEntity, TTableEntity>.Copy(updated, toSave);
                    //tosave.idstatus == 20 ! 
        toSave.DateMod = DateTime.Now;
        toSave.UserMod = (short)userid;

        _dbContext.Update(toSave);
        _dbcontext.SaveChanges();//after save => idstatus == null
    }

我在

编辑核心版本更新

EFCore 更新为预览版 9,

.Net Core 3.0 更新为预览版 9

同样的错误

编辑: 更新的对象没有跟踪,所以我需要对同一个对象进行观察,然后将道具复制到它 => 然后保存

之前'SaveChanges()'idstatus还是20

编辑:PropertyCopier 代码

public static class PropertyCopier<TSource, TTarget> where TSource : class where TTarget : class
{
    public static void Copy(TSource source, TTarget target)
    {
        var sourceProperties = source.GetType().GetProperties();
        var targetProperties = target.GetType().GetProperties();

        foreach (var sourceProperty in sourceProperties)
        {
            var targetProperty = targetProperties.SingleOrDefault(x =>
                sourceProperty.Name.ToLower() == x.Name.ToLower() && sourceProperty.PropertyType == x.PropertyType);
            if (targetProperty == null)
                continue;
            var value = sourceProperty.GetValue(source);
            targetProperty.SetValue(target, value);
        }
    }
}

编辑:示例 我尝试手动设置 toSave.IdStatus(无 propCopier),并使用 db 中保存的相同值(这意味着我不想更改值 db.idstatus = 20 / tosave.idstatus = 20)。保存更改后我有 db.idstatus = null (A)

如果我再次尝试更新(null 为 20)=> 成功,db.idstatus = 20 (B)

Seabizkit 的编辑 2:

public interface ISimpleEntity
{
    int Idnrr { get; set; }
}
*public interface IBaseEntity : ISimpleEntity
{
    short? UserIns { get; set; }
    DateTime? DateIns { get; set; }
    short? UserMod { get; set; }
    DateTime? DateMod { get; set; }
}
public partial class Tbl : IBaseEntity { }//i have to do this in other file
public partial class Tbl
{

    *public int Idnrr { get; set; }
    public string Description { get; set; }
    public DateTime? startDate { get; set; }
    public DateTime? endDate { get; set; }
    public short? IdStatus { get; set; }
    public byte? id2{ get; set; }
    public short? IdRequester { get; set; }
    *public short? UserIns { get; set; }
    *public DateTime? DateIns { get; set; }
    *public short? UserMod { get; set; }
    *public DateTime? DateMod { get; set; }
    //other 140 public props
}

编辑:"again"

我试图将状态值从 20 更改为 12 => updated.status = 12,在 propscopy 之后 tosave.status = 12(在复制之前是 20)=> SaveChanges() db.status = 12 好的,有效 (C)

但是

如果我需要更新一些其他值,或再次执行保存,在 (C) 之后(使用 updated.status=12 和 db.status=12 )=> 在 propscopy toSave.status 之后=12(之前的事件) => saveChanges() db/tosave status 属性变为空 (D)

图片:调试 idStatus Prop 的 propCopier 方法 => 它有效,即使使用其他道具,甚至 same/or 不是 idstatus 值 (A,B)

TEST A 有效(复制评论)

TEST B f*****k(复制未注释)即使我手动设置了 idstatus (tosave.idstatus = updated.idstatus)

避免使用

PropertyCopier<TTableEntity, TTableEntity>.Copy(updated, toSave);

并手动将updated实体的属性一一分配给toSave