MVC 3 프로젝트에서이 작업을 수행하는 더 쉬운 방법이 있었으면합니다. 내 데이터베이스에는 LINQ2SQL을 통해 내 응용 프로그램에 매핑되는 Customer 테이블이 있습니다. 내가 업데이트를 수행 부분 고객 클래스, 룩업 등도 있습니다 -이 같은 업데이트 방법이 있습니다LINQ2SQL 엔터티 - 변경된 필드 만 업데이트
나는 필드를 업데이트 할 수있는 덜 성가신 방법이었다 찾으려는 무엇public static void Update(Customer customer)
{
if (customer == null)
return;
using(var db = new DataBaseContext)
{
var newCustomer = db.Customers.Where(c => c.customer_id = customer.customer_id).SingleOrDefault();
if(newCustomer == null)
return;
newCustomer.first_nm = customer.first_nm;
// ...
// ... Lot's of fields to update
// ...
newCustomer.phone_num = customer.phone_nm;
db.SubmitChanges();
}
}
고객의 해당 필드가 다른 newCustomer에 있습니다.
제안 사항? 감사.
public class Customer
{
public string first_nm { get; set; }
public int phone_num { get; set; }
}
class CustomerComparer : IEqualityComparer<Customer>
{
public bool Equals(Customer x, Customer y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the customer' properties are equal.
return x.first_nm == y.first_nm && x.phone_num == y.phone_num ;
}
}
을하고 다음과 같이 수행합니다 :
내 코드가 attach 메소드를 가져 오지만 실패합니다. "버전 구성원을 선언했거나 업데이트 확인 정책이없는 경우 엔티티를 원래 상태로만 수정하여 첨부 할 수 있습니다." 오류 –
답변을 업데이트했습니다. – Kamyar