2017-02-23 3 views
0

Azure 테이블 저장소에 저장 한 일부 레코드의 유형을 나타내는 클래스입니다.Azure 테이블 저장소에서 레코드를 업데이트하기 위해 동일한 유형의 두 인스턴스를 병합하는 방법

public class UserType : TableEntity 
{ 
    public enum eGenderType { None, Male, Female } 
    public virtual DateTime BornDate { get; set; } 
    public Guid ConfirmationCode { get; set; } 
    public virtual string Email { get; set; } 
    public virtual bool? Enabled { get; set; } 
    public virtual string FirstName { get; set; } 
    public virtual eGenderType Gender { get; set; } 
    public virtual string Surname { get; set; } 

    public UserType Get() 
    { 
     TableQuery<UserType> Query = new TableQuery<UserType>().Where("PartitionKey eq '" + Email + "' and RowKey eq '" + ConfirmationCode + "'"); 
     UserType User = GetFromTable(Query).FirstOrDefault(); 

     Type InstanceType = GetType(); 
     foreach (PropertyInfo Property in typeof(UserType).GetProperties(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) 
     { 
      PropertyInfo InstanceProperty = InstanceType.GetProperty(Property.Name, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); 
      if (Property.PropertyType.IsValueType && Equals(InstanceProperty.GetValue(this), Activator.CreateInstance(Property.PropertyType))) 
       InstanceProperty.SetValue(this, Property.GetValue(User)); 
      else if (InstanceProperty.GetValue(this) == null) 
       InstanceProperty.SetValue(this, Property.GetValue(User)); 
     } 
     return this; 
    } 

    public void Update() 
    { 
     SaveToTable(); // Perform an InsertOrReplace operation on Azure Table Storage 
    } 
} 

public class UserModel : UserType 
{ 
    public void SetFullName() 
    { 
     Get(); 
     base.Update(); 
    } 
} 

그런 다음 HTTP POST 요청에서 데이터를 가져와 데이터 모델로 사용되는 UserModel 객체에 바인딩하는 MVC 작업이 있습니다.

[HttpPost] 
public void SetFullName(UserModel Model) 
{ 
    Model.SetFullName(); 
} 

내가 전체 레코드를 업데이트 할 필요가 푸른 표 저장소에 레코드를 업데이트하려면 내가 모델에서 한 번 새로운 업데이트 이전의 데이터가 필요하므로 나는 하나의 필드를 업데이트 할 수 없습니다. 이 경우 Model 객체에서 값이있는 속성 (전자 메일, ConfirmationCode, 이름 및 성) 만 있습니다. base.Update가 Table에서 값을 가져 오는 범위를 갖기 전에 호출하는 Get() 메서드를 값이없는 속성의 Model 개체에 넣습니다.

테이블 이름과성에 null이 있고 모델에 값이있는 경우 업데이트 작동 및 레코드가 이전 값과 새 값으로 올바르게 업데이트됩니다. 하지만 다른 경우에는 테이블 이름과성에 값이 있고 모델에 null 인 경우 Get 메소드는 Model 객체에 이전 성과 이름을 null 값 대신 업데이트 후 테이블에 넣습니다. 항상 오래된 가치를 지녔습니다.

그래서 올바른 방법은 무엇입니까? 내 접근 방식이 맞습니까?

답변

0

else if (InstanceProperty.GetValue(this) == null) InstanceProperty.SetValue(this, Property.GetValue(User));

이 코드는 모델 속성의 값이 null의 경우, 그것은 사용자합니다 (그 커다란에서 가져 오기) prpoerty의 값을 설정하는 것을 의미합니다. FirstName과 LastName이 null이면 이전 모델의 FirstName과 LastName으로 바뀝니다.

이전 모델의 성과 이름을 설정하지 않으려면 if 조건을 작성하여 속성 이름을 확인할 수있는 것이 좋습니다. 이름이 "FirstName"또는 "Surname"이면 SetValue 메서드를 호출하지 않습니다.

자세한 내용은 아래 코드를 참조하십시오. 알림 :이 코드는 firstname 또는 lastname 속성 만 업데이트하려고합니다.

public UserType Get() 
    { 
     TableQuery<UserType> Query = new TableQuery<UserType>().Where("PartitionKey eq '" + Email + "' and RowKey eq '" + ConfirmationCode + "'"); 
     UserType User = GetFromTable(Query).FirstOrDefault(); 

     Type InstanceType = GetType(); 
     foreach (PropertyInfo Property in typeof(UserType).GetProperties(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) 
     { 
      PropertyInfo InstanceProperty = InstanceType.GetProperty(Property.Name, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); 
      if (Property.PropertyType.IsValueType && Equals(InstanceProperty.GetValue(this), Activator.CreateInstance(Property.PropertyType))) 
       InstanceProperty.SetValue(this, Property.GetValue(User)); 
      else if (InstanceProperty.GetValue(this) == null) { 
       if (InstanceProperty.Name != "FirstName" && InstanceProperty.Name != "Surname") 
       { 
        InstanceProperty.SetValue(this, Property.GetValue(User)); 
       } 
      } 
     } 
     return this; 
    } 

결과 : 테이블에서 enter image description here 새로운 모델 가져 오기 : 설정 값 후

enter image description here

을 : enter image description here

을 게다가, 당신은 전체 개체를 업데이트 할 경우, 먼저 로컬로 엔티티를 가져와야하고, 마침내 엔티티와 새 MVC 모델을 비교해야합니다. 그것을 업데이트하십시오.