0

나는 UpdateAsync을 호출 중이지만,이 값을 전달하지 않아도 CreationTimeCreatorUserId 열을 업데이트합니다. 필수 열만 업데이트해야합니다. CreationTime 및 CreatorUserId가 UpdateAsync에서 설정되는 이유는 무엇입니까?

public async Task UpdateTest(TestDetailsDTO input) 
{ 
    var classobj = ObjectMapper.Map<Test>(input); 
    await UpdateAsync(classobj); 
} 
public class TestDetailsDTO : FullAuditedEntityDto 
{ 
    public string TestCode { get; set; } 
    public string TestName { get; set; } 
    public string TestDesc { get; set; } 
} 

{ 
    "testCode": "string", 
    "testName": "string", 
    "testDesc": "string", 
    "id": 1 
} 
매개 변수 inputUpdateTest 서비스 방법에 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; } 
} 
+0

실수로'UpdateAsync' 메소드,'TestDetailsDTO' 및'Test' 클래스를 표시하는 것을 잊었습니다. –

답변

0

현재 흐름 :

  1. ObjectMapper.Map<Test>(input) 새로운 Test 개체를 만듭니다.
  2. CreationTimeCreatorUserIddefault(DateTime)default(long?)이다.
  3. ABP는 이러한 값을 설정합니다.

올바른 흐름 :

  1. 데이터베이스에서 classobj를 가져옵니다.
  2. 복원 CreationTimeCreatorUserId.
  3. 지도 input to classobj.

var classobj = repository.GetEntity(input.id); // 1 
input.CreationTime = classobj.CreationTime; // 2 
input.CreatorUserId = classobj.CreatorUserId; // 2 
ObjectMapper.Map(input, classobj);    // 3 

더 나은 디자인 :

  • input에 대한 FullAuditedEntityDto가 → 2 단계

를 건너 상속하지 마십시오 다른 방법으로도 작동합니까? 그것은 여분의 GetEntity 메서드를 호출하기 때문입니다.

다른 방법은 attach입니다. 트레이드 오프는 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(); 
+0

FullAuditedEntityDto에서 상속하지 않으면 다른 모든 열을 수동으로 전달해야합니다. 그리고 코드를 변경해야합니다. 업데이트 요청에 새로운 열을 추가하려는 경우 –

+0

다른 방식으로 작동합니다. 그것은 여분의 GetEntity 메서드를 호출하기 때문입니다. 그리고 나는 모든 테이블에 대해 동일한 변경을해야합니다. –

+0

하지만 링크의 예가 명확하지 않습니다. –