C# 为什么CreationTime和CreatorUserId设置为UpdateAsync?

C# 为什么CreationTime和CreatorUserId设置为UpdateAsync?,c#,entity-framework,audit,aspnetboilerplate,change-tracking,C#,Entity Framework,Audit,Aspnetboilerplate,Change Tracking,我正在调用UpdateAsync,但它正在更新CreationTime和CreatorUserId列,即使我没有传入这些值。它应该只更新所需的列 { "testCode": "string", "testName": "string", "testDesc": "string", "id": 1 } 参数input获取UpdateTest服务方法中的CreationTime public class Test : FullAuditedEntity { public c

我正在调用
UpdateAsync
,但它正在更新
CreationTime
CreatorUserId
列,即使我没有传入这些值。它应该只更新所需的列

{
  "testCode": "string",
  "testName": "string",
  "testDesc": "string",
  "id": 1
}
参数
input
获取
UpdateTest
服务方法中的
CreationTime

public class Test : FullAuditedEntity
{
    public const int NVarcharLength20 = 20;
    public const int NVarcharLength30 = 30;
    public const int NVarcharLength50 = 50;

    [Required]
    [MaxLength(NVarcharLength20)]
    public virtual string TestCode { get; set; }

    [Required]
    [MaxLength(NVarcharLength30)]
    public virtual string TestName { get; set; }

    [MaxLength(NVarcharLength50)]
    public virtual string TestDesc { get; set; }
}
电流:

  • ObjectMapper.Map(输入)
    创建一个新的
    Test
    对象
  • CreationTime
    CreatorUserId
    default(DateTime)
    default(long?)
  • ABP设置这些值
  • 正确流量:

  • 从数据库中获取
    classobj
  • 还原
    CreationTime
    CreatorUserId
  • input
    映射到
    classobj
  • 更好的设计:

    • 不要为
      输入继承
      fullAuditedEntityTo
      → 跳过步骤2

    它起作用了,还有别的办法吗?因为它正在调用额外的GetEntity方法

    另一种方法是。取舍是必须显式映射,而不是
    ObjectMapper.map

    // Create new stub with correct id and attach to context.
    var classobj = new Test { Id = input.Id };
    repository.As<EfRepositoryBase<MyDbContext, Test>>().Table.Attach(classobj);
    
    // Now the entity is being tracked by EF, update required properties.
    classobj.TestCode = input.TestCode;
    classobj.TestName = input.TestName;
    classobj.TestDesc = input.TestDesc;
    
    // EF knows only to update the properties specified above.
    _unitOfWorkManager.Current.SaveChanges();
    
    //创建具有正确id的新存根并附加到上下文。
    var classobj=newtest{Id=input.Id};
    repository.As().Table.Attach(classobj);
    //现在EF正在跟踪实体,请更新所需属性。
    classobj.TestCode=input.TestCode;
    classobj.TestName=input.TestName;
    classobj.TestDesc=input.TestDesc;
    //EF只知道更新上面指定的属性。
    _unitOfWorkManager.Current.SaveChanges();
    
    public class Test : FullAuditedEntity
    {
        public const int NVarcharLength20 = 20;
        public const int NVarcharLength30 = 30;
        public const int NVarcharLength50 = 50;
    
        [Required]
        [MaxLength(NVarcharLength20)]
        public virtual string TestCode { get; set; }
    
        [Required]
        [MaxLength(NVarcharLength30)]
        public virtual string TestName { get; set; }
    
        [MaxLength(NVarcharLength50)]
        public virtual string TestDesc { get; set; }
    }
    
    var classobj = repository.GetEntity(input.id); // 1
    input.CreationTime = classobj.CreationTime;    // 2
    input.CreatorUserId = classobj.CreatorUserId;  // 2
    ObjectMapper.Map(input, classobj);             // 3
    
    // Create new stub with correct id and attach to context.
    var classobj = new Test { Id = input.Id };
    repository.As<EfRepositoryBase<MyDbContext, Test>>().Table.Attach(classobj);
    
    // Now the entity is being tracked by EF, update required properties.
    classobj.TestCode = input.TestCode;
    classobj.TestName = input.TestName;
    classobj.TestDesc = input.TestDesc;
    
    // EF knows only to update the properties specified above.
    _unitOfWorkManager.Current.SaveChanges();