Order 개체가있는 Customer 개체가 있다고 가정 해 보겠습니다. Order 개체에는 OrderDetail 개체가 있습니다.Entity Framework 4.1에서 엔터티 상태 변경
Customer oCustomer
using(var context = new MyContext)
{
oCustomer = context.Include("Order.OrderDetail").Find(1);
}
oCustomer.Name ="blah blah";
oCustomer.Order.Description = "blah blah";
oCustomer.Order.OrderDetail.Quantity = 10;
는 이제 다음과 같은 고객의 상태를 변경할 때 :
using(var context = new MyContext)
{
context.Entry(oCustomer).State = EntityState.Modified.
context.SaveChanges();
}
이는 oCustomer 객체가 아닌 oCustomer에있는 주문 및 OrderDetail을 절약 할 수 있습니다. context.Entry (oCustomer) .State = EntityState.Modified는 Order 및 OrderDetail이 아닌 oCustomer의 상태 만 변경합니다. 현재 ObjectGraph에서 각 엔티티의 상태를 수동으로 변경해야 변경 사항이 저장됩니다. 상위 엔티티 대신 전체 ObjectGraph의 상태를 변경할 수있는 방법이 있습니까? 확장 방법이나 그 밖의 다른 방법이 있습니까?
감사합니다. 그것은 작동! 그러나 그것은 우아한 솔루션입니까? 엔티티를 저장할 때마다 엔티티가 다시로드됩니다. – Baig
엔티티에 대한 모든 정보가 있다면 새로 만들 수 있습니다. 모든 엔티티를 컨텍스트에 첨부하고 모두 수정하도록 설정 한 다음 savechanges를 호출하십시오. http://stackoverflow.com/questions/7001602/asp-net-mvc-with-ef-4-1-navigation-properties/7008849#7008849는 간단한 시나리오에서만 작동합니다. – BennyM