2017-11-30 5 views
0

다른 테이블 RFM_NOTD_NotificationsDetail에 대한 참조가있는 RFM_NOTF_Notifications이라는 테이블이 있습니다. EF를 사용하여 데이터베이스와 통신하고 NotificationsDetail 엔티티의 목록은 Notifications입니다. 내가 Notifications 개체의 목록을 업데이트하려고엔티티/모델 업데이트 중 EF 오류 : FOREIGN KEY 제약 조건과 충돌하는 UPDATE 문

,이 오류 받고 있어요 :

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_RFM_NOTF_Notifications_RFM_NOTD_NotificationsDetail". The conflict occurred in database "RegulatoryFileManagementScrumQA", table "dbo.RFM_NOTD_NotificationsDetail", column 'RFM_NOTD_P_NotificationDetailID'

을 그리고 이것은 저장소 안에 추가/업데이트 방법 :

private void CreateNotifications(GeneralBO file, RFM_NOTD_NotificationsDetail notfFile) 
{ 
     foreach (NotificationsBO notf in file.Notifications) 
     { 
      RFM_NOTF_Notifications nt = new RFM_NOTF_Notifications 
      { 
       RFM_NOTF_B_Description = notf.Description 
      }; 

      if(notf.Id > 0) // Save 
      { 
       nt.RFM_NOTF_P_NotificationID = notf.Id; 
       nt.RFM_NOTD_F_NotificationDetailID = file.Id; 

       nt.RFM_NOTF_M_ModifiedById = file.CreatedById; 
       nt.RFM_NOTF_M_ModifiedDateTime = DateTime.Now; 
       new GenericRepository<RFM_NOTF_Notifications>(_dbContext).update(nt);      
      } 
      else 
      { 
       nt.RFM_NOTF_M_CreatedById = file.CreatedById; 
       nt.RFM_NOTF_M_CreatedDateTime = DateTime.Now; 
       notfFile.RFM_NOTF_Notifications.Add(nt); 
      } 
     } 
} 

GeneralBO가있다 UI에서 데이터를 보유하는 모델/객체.

는 그리고이 비즈니스 오브젝트/모델에게

public class NotificationsBO 
{ 
    public long Id { get; set; } 
    public string Description { get; set; } 
    public int CreatedById { get; set; } 
    public DateTime CreatedDateTime { get; set; } 
    public int ModifiedById { get; set; } 
    public DateTime ModifiedDateTime { get; set; } 
} 

public class NotificationDetailsBO : ApplicationBO 
{ 
    public long Id { get; set; } 
    ....... 
    ....... 
    public List<NotificationsBO> Notifications { get; set; } 
} 

입니다 그리고 내 테이블은 다음과 같습니다

enter image description here

답변

1

을 구현하는 엔티티 프레임 워크의 EntityStateModified 속성을 사용해야합니다.

뭔가가 내 관심을 끌었습니다. 이름이 RFM_NOTD_NotificationsDetail notfFile 인 메서드가 전달되는 매개 변수가 있습니다.나는 그 자료가 어디서 왔는지 그 자료를 알지 못한다.

nt.RFM_NOTD_F_NotificationDetailID = notfFile.Id; 
+0

감사합니다. – Sandy

0

는 내가 먼저 해당 오류가 의미하는 무엇을 설명해 보자.

The Update statement conflicted with the Foreign Key

그것은 단순히 테이블 Notifications의 기본 키가 충돌이 명확하게 당신이 그것을 변경할 수 있도록 Notification Detail 테이블의 외래 키는 것을 의미한다.

다음은 시도 할 수있는 몇 가지 해결책입니다.

  • 먼저 알림 세부 사항을 분리 한 다음 알림

을 제거하거나 더 나은 솔루션 캐스케이드에서 값을 삭제하는 삭제 구현하는

  • 유창함 API를 사용하는 것입니다 (권장하지 않음) 알림 세부 사항 테이블에서 데이터를 삭제하자 마자 알림 테이블

당신은

ALTER TABLE TableName 
ADD CONSTRAINT [New_FK_Constraint] 
FOREIGN KEY (ColumnName) REFERENCES SecondTable(ColumnName) 
ON DELETE CASCADE ON UPDATE CASCADE 
GO 

및 갱신의 경우에 사용하여 데이터베이스에서 같은를 얻을 수 있습니다.

당신은 더 나은 업데이트 기능을 내가 nt.RFM_NOTD_F_NotificationDetailID = file.Id; 것을 내가 file.IdNotificationDetails 테이블에 레코드가없는 생각 의심