2013-06-17 4 views
3

나는 ASPxGridViewObjectDataSource을 포함하는 ASP.NET 웹폼 페이지가 있습니다의 GridView

public class Employee 
{ 
    public int EmployeeId { get; set; } 

    public string Name { get; set; } 

    public string Email { get; 

    public string Password { get; set; } 

    public string Telephone { get; set; } 
} 

ObjectDataSourceUpdate 메서드를 호출

<dx:ASPxGridView ID="gvEmployees" runat="server" 
    AutoGenerateColumns="False" DataSourceID="objDsEmployees" 
    KeyFieldName="EmployeeId"> 
    <Columns> 
     <dx:GridViewCommandColumn VisibleIndex="0"> 
      <EditButton Visible="True" /> 
     </dx:GridViewCommandColumn> 
     <dx:GridViewDataTextColumn FieldName="EmployeeId" VisibleIndex="1" /> 
     <dx:GridViewDataTextColumn FieldName="Name" VisibleIndex="2" /> 
     <dx:GridViewDataTextColumn FieldName="Email" VisibleIndex="3" /> 
     <dx:GridViewDataTextColumn FieldName="Telephone" VisibleIndex="5" /> 
    </Columns> 
</dx:ASPxGridView> 
<asp:ObjectDataSource ID="objDsEmployees" 
    runat="server" 
    ConflictDetection="CompareAllValues" 
    DataObjectTypeName="MySite.Data.Models.Employee" 
    DeleteMethod="Delete" 
    OldValuesParameterFormatString="original{0}" 
    InsertMethod="Insert" 
    SelectMethod="GetAll" 
    TypeName="MySite.Services.EmployeeService" 
    UpdateMethod="Update" /> 

직원 모델은 다음과 같은 속성을 포함을 서비스 계층에서 :

public void Update(Employee employee, Employee originalEmployee) 
{ 
    _db.Employees.Attach(originalEmployee); 
    _db.Entry(originalEmployee).CurrentValues.SetValues(employee); 
    _db.SaveChanges(); 
} 

Update 메서드를 호출하면 매개 변수 employeeoriginalEmployee에는 ASPxGridView의 열로 사용되는 속성 만 포함되고 다른 속성 (예 : Password)은 null입니다.

어딘가에 이러한 값을 보존 할 수있는 방법이 있습니까? 그건 그렇고, 나는 데이터 액세스를위한 엔티티 프레임 워크와 GridView의 DevExpress를 사용한다. 있습니다

public void Update(int employeeId, string name, string email) 
{ 
    var employee = GetById(employeeId); 
    employee.Name = name; 
    employee.Email = email; 

    _db.SaveChanges(); 
} 

다른 솔루션 그러나보다 더 환영합니다 :에 서비스 계층에서

<asp:ObjectDataSource 
    ID="objDsEmployees" 
    runat="server" 
    DeleteMethod="Delete" 
    InsertMethod="Insert" 
    SelectMethod="GetAll" 
    TypeName="MySite.Services.EmployeeService" 
    UpdateMethod="Update"> 
    <UpdateParameters> 
     <asp:Parameter Name="employeeId" Type="Int32" /> 
     <asp:Parameter Name="name" Type="String" /> 
     <asp:Parameter Name="email" Type="String" /> 
    </UpdateParameters> 
</asp:ObjectDataSource> 

그리고 Update 방법 :

답변

2

또한 ObjectDataSourceConflictDetection="CompareAllValues"를 설정하여이 문제를 해결 할 수 있습니다

<asp:ObjectDataSource ID="objDsEmployees" 
    runat="server" 
    DeleteMethod="Delete" 
    InsertMethod="Insert" 
    SelectMethod="GetAll" 
    UpdateMethod="Update" 
    DataObjectTypeName="App.Core.Domain.Employee" 
    TypeName="App.Core.Services.EmployeeService" 
    OldValuesParameterFormatString="original{0}" 
    ConflictDetection="CompareAllValues" /> 

그런 다음 우리가 ID로 먼저 원래 직원을 가져 EmployeeServiceUpdate 방법으로, 우리는 업데이트 된 직원과 함께이 원래 직원을 병합 . 이렇게하면 데이터베이스에 대한 추가 호출이 필요하지만 업데이트되지 않은 속성은 null이되지 않습니다.

public void Update(Employee originalEmployee, Employee employee) 
{ 
    // Get the original employee by its id. 
    Employee fullOriginalEmployee = GetById(originalEmployee.EmployeeId); 

    // Merge the data of the updated employee with the original employee. 
    fullOriginalEmployee.Name = employee.Name; 
    fullOriginalEmployee.Email = employee.Email; 
    fullOriginalEmployee.Telephone = employee.Telephone; 
    fullOriginalEmployee.BirthDate = employee.BirthDate; 

    // Persist changes in database. 
    SaveChanges(); 
} 
0

은 내가 ObjectDataSource을 변경하여이 문제를 해결!