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?)
classobj
CreationTime
和CreatorUserId
input
映射到classobj
- 不要为
输入继承
→ 跳过步骤2fullAuditedEntityTo
它起作用了,还有别的办法吗?因为它正在调用额外的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();