2009-12-22 4 views
7

2 개의 테이블이 있으며 Table1에는 기본 키 'CustomizationId'가 있으며 Table2에는 이에 맞는 FK Customizationid가 있습니다. Table2에는 기본 키가 없습니다.ADO.Net Entity Model의 다른 테이블에 외래 키가있는 테이블을 어떻게 업데이트합니까?

웹 기반 양식에서 새 레코드를 추가하려고합니다. 나는 데이터베이스에이를 저장하려고 내가 오류 얻을 : 내가 주석 라인에 추가할지 여부를 내가 오류를 얻고있다 SaveChanges를 호출하면

  Customization customization = new Customization(); 
      Code code = new Code(); 
      customization.Name = CustomizationName.Text;    
      customization.LastUpdated = DateTime.Now; 

      code.Top = top_js.InnerText; 
      code.Bottom = bottom_js.InnerText; 

      //code.CustomizationId = customization.CustomizationId; 

      customization.Code = code;    
      entities.AddToCustomizations(customization); 
      entities.SaveChanges(); 

합니다.

Unable to update the EntitySet 'Code' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation. 

이 상황을 어떻게 처리합니까? 사용자 정의에 추가하는 것과 동시에 코드를 추가하기 만하면됩니다. '코드'테이블에는 Customization이 Customization에 의해 설정된 PK/Identity로 설정되어야합니다.

아이디어가 있으십니까?

답변

7

EF와 관련하여 PK가없는 표는보기입니다.

  1. 약간의 거짓말을 알려하고, 'view' is a table
  2. Add modification functions (삽입/업데이트/삭제) EF 설득 그래서 당신이 그들을 편집 할 수 있습니다

    는이 두 가지 옵션을 의미합니다.

일반적으로 ...

수정 기능은 저장 프로 시저를하고 있지만 데이터베이스에 액세스 할 수없는 경우가 실제로 SSDL에 직접 T-SQL을 추가 할 수 있습니다 즉, 각 작업 (삽입, 업데이트 및 삭제)에 대한 EDMX의 <StorageModel> 요소에서이 같은 : 필요에 따라 당신이 당신의 SSDL의 수정 기능이 있으면

<Function Name="InsertCode" BuiltIn="false" IsComposable="false" > 
     <CommandText> 
       INSERT dbo.TCode(ID, Name) VALUES (@ID, @Name) 
     </CommandText> 
     <Parameter Name="ID" Type="int" Mode="In" /> 
     <Parameter Name="ID" Type="nvarchar(max)" Mode="In" /> 
</Function> 

당신이 map them 필요 그래서 EF 그들을 사용.

나는 (1)을 권장합니다.

희망이 도움이됩니다.

건배 알렉스

관련 문제