2011-08-23 5 views
2

Entity Framework (MySQL/Connector)를 통해 외래 키 관계가있는 객체를 삭제하려고합니다.MySQL + Entity Framework : 객체를 삭제할 수 없습니다.

ClientAccount >>> ClientEmailAddresses

foreach (ClientAccount client in recsClientStore.Deleted) { 
     ClientAccount stub = new ClientAccount(); 
     stub.Id = client.Id; 
     this.DBContext.AttachTo("ClientAccounts", stub); 


     stub.FkClientEmailAddresses.Clear(); 
     this.DBContext.DeleteObject(stub); 
    } 

    this.DBContext.SaveChanges(); 

..하지만 난 다음 오류 얻을이 수행 할 때

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

나는이 나를 잎 어디 정말 모르겠어요합니다. 먼저 EmailAddress 객체를 삭제해야합니까? 우리는 캐스케이드 (cascade)를 돌보는 것을 경계하고 있지만 외래 키를 정리하기 위해 점점 더 필요해 보이는 것처럼 보입니다.

stub.FkClientEmailAddresses.Clear(); 

관계를 삭제하지 않습니다

답변

2

문제는이 때문이다. 관련 엔터티의 FK 만 null로 설정합니다. 당신이 원하는 경우에 그들이 정말 삭제하려면 다음 중 하나를 수행해야

  • 자신의 객체가 identifying relationship
  • 변경 관계 설정에 Remove를 호출하여 삭제 -
  • 올바르게 설정 캐스케이드 모두 EDMX에서 삭제 예상대로 Clear 작업을 할 것 및 데이터베이스를 호출하고 전혀 지우지 마십시오
+0

감사합니다. 우리가 실제로 계단식이 필요한지 여부를 확인하기 위해 구조를 다시 검토 할 것입니다. – pierre