2011-02-16 2 views
3

DataContext.ApplyCurrentValues ​​()에는 entitySetName이 필요합니다. Entity Framework에서 개체를 편집하는 방법?

는 그 코드가 같은 것이라고 생각 : 그것은 올바른

public void Edit(Products p) 
    { 
     DataContext.ApplyCurrentValues("Products", p); 
     DataContext.SaveChanges(); 
    } 

인가?

+0

http://blogs.msdn.com/b/wriju/archive/2009/04/24/ado-net-entity-framework-editing-a-detached-object.aspx –

답변

0

당신이 EF4을 사용하고 - 다음은 엔티티 세트 이름에 대해 걱정할 필요가 없습니다 대신 이렇게 :

DataContext.Product.ApplyCurrentValues(p); 
+0

메서드 ApplyCurrentValues에 대한 오버로드가 발생하지 않습니다 1 인수들 – FreeVice

3

을이 닷넷이 예를 들어

4.0, 우리 가정 할 수 있습니다 Product 객체를 다루고 있습니다.

using (DBEntities context = new DBEntities()) 
{ 
    //Must attach first and change the state to modified 
    context.Products.Attach(product); 

    //If you are using .Net 4.1 then you can use this line instead: 
    //context.Entry(
    context.ObjectStateManager.ChangeObjectState(product, EntityState.Modified); 

    context.SaveChanges(); 
} 

여기에 본 당신이 당신 대신 "context.ObjectStateManager.ChangeObjectState (제품, EntityState.Modified)"의 "context.Entry (...)"을 사용할 수 있습니다 닷넷 4.1을 사용하는 경우 : Example of context.Entry()

가장 직접적인 방법입니다. 먼저 DB에서 객체를 가져올 필요가 없습니다. 사용자가 수정하려는 객체를 제공하면됩니다. 유일한 단점은이 단일 필드가 아니라 모든 필드를 업데이트한다는 것입니다.

관련 문제