2017-10-03 1 views
1

많은 하위 엔티티가있는 거대한 양식이 있으므로 Automapper를 사용하여 EF 오브젝트를 트리로 채 웁니다. 그럼 DB에서 비슷한 엔티티를 업데이 트하고 싶습니다. 컨텍스트에 연결하는 방법은 무엇입니까?EF에 트리가있는 엔티티를 첨부하십시오.

"Attaching an entity of type 'Infrastructure.Asset.ApplicationDriver' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate."

내가 시도 :

 // ApplicationDriver has many child objects 
     Infrastructure.Asset.ApplicationDriver driver = mapper.Map<Infrastructure.Asset.ApplicationDriver>(model); 

     // get similar object from DB 
     Infrastructure.Asset.ApplicationDriver currentApplication = db.ApplicationDrivers.Where(p => p.ApplicationId == model.ApplicationId).FirstOrDefault(); 

     if (currentApplication == null) 
     { 
      db.ApplicationDrivers.Add(driver); 
     } 
     else 
     { 
      // try to attach driver to current context. 
      // I want to 'replace' current object with all child objects in DB 
      db.ApplicationDrivers.Attach(driver); 
      currentApplication = driver; 
     } 
     await db.SaveChangesAsync(); 

나는 오류가 발생 :

 var currentApplication = db.ApplicationDrivers.Where(p => p.ApplicationId == model.ApplicationId).FirstOrDefault(); 

     if (currentApplication == null) 
     { 
      db.ApplicationDrivers.Add(driver); 
     } 
     else 
     { 
      driver.Id = currentApplication.Id; 
      db.ApplicationDrivers.Attach(driver); 
     } 
     await db.SaveChangesAsync(); 

과 같은 오류가

나는 방법으로 그것을하려고합니다. 올바르지 않은 것은 무엇이며 드라이버에서 currentApplication으로의 모든 자식 객체에 대한 각 속성을 수동으로 복사하지 않고이 문제를 해결하는 방법은 무엇입니까?

+0

https://www.nuget.org/packages/RefactorThis.GraphDiff/ –

+0

@IvanStoev. 아주 이상한 오류 : datetime2 데이터 형식을 datetime 데이터 형식으로 변환하면 범위를 벗어나는 값이 발생했습니다. \ r \ n 문이 종료되었습니다 –

+0

이것은 원래 문제와 관련이없는 것 같습니다. 해결 했니? –

답변

0

나는 기사를 발견했다 DTO를 편집 사례로 EF에 매핑하는 데 오토마터를 사용하지 않는 것이 좋습니다.

0

필요가 없습니다 당신이 DB를 업데이트 할 경우, 엔티티에 부착하고, 현재의 객체로 DB를 대체 할 수는 :

https://www.devtrends.co.uk/blog/stop-using-automapper-in-your-data-access-code

:

// ApplicationDriver has many child objects 
    Infrastructure.Asset.ApplicationDriver driver = mapper.Map<Infrastructure.Asset.ApplicationDriver>(model); 

    // get similar object from DB 
    Infrastructure.Asset.ApplicationDriver currentApplication = db.ApplicationDrivers.Where(p => p.ApplicationId == model.ApplicationId).FirstOrDefault(); 

    if (currentApplication == null) 
    { 
     db.ApplicationDrivers.Add(driver); 
    } 
    else 
    { 
     //this will attach, and set state to modified 
     //same as previous question, make sure you virtual properties are not populated to avoid unwanted duplicates. 
     db.Entry(driver).State = System.Data.Entity.EntityState.Modified; 
     currentApplication = driver; 
    } 
    await db.SaveChangesAsync(); 
+0

동일한 오류가 발생합니다. –

관련 문제