2011-08-03 5 views
0

asp.net 3.5 sp1에서 작업 중입니다. 두 개의 테이블이 있습니다삽입 또는 업데이트시 EntityDataModel 부모 키가 null로 설정 됨

User 
    UserID PK (int) 
    UserName (varchar) 
    ContactInfoID FK (int) Allow Null 

ContactInfo 
    ConactInfoID PK (int) 
    Address (varchar) 
    City (varchar) 
    State (varchar) 
    Zip (varchar) 

EDM을 사용하여 해당 부모가없는 ContactInfo가없는 사용자를 삽입/업데이트하려고합니다. 데이터베이스가이를 허용합니다.

null 부모를 나타 내기 위해 ContactInfo_ContactInfoID = -1을 설정할 수 있기를 원합니다. 사용자 부분 클래스에 다음 함수가 있습니다. null에 EntityKey의 다른 측면을 설정하려고 시도했을 때 발생하는 오류가 포함되었습니다.

 public int ContactInfo_ContactInfoID 
     { 
      get 
      { 
       if (this.ContactInfoReference == null) 
        return -1; 

       if (this.ContactInfoReference.EntityKey == null) 
        return -1; 

       return (int)this.ContactInfoReference.EntityKey.EntityKeyValues [0].Value; 
      } 
      set 
      { 
       //To notify others listening that the property is changing 
       this.OnPropertyChanging ("ContactInfoReference_ContactInfoD"); 

       if (value != -1) 
       { 
        //Always needs to create a new EntityKey object 
        this.ContactInfoReference.EntityKey = 
         new EntityKey (
          "ProjectRedEntities.ContactInfo", 
          new EntityKeyMember [] 
           { new EntityKeyMember("ContactInfoID", 
            (object)value) }); 
       } 
       else 
       { 
//     this.ContactInfoReference.EntityKey = null; 
        // ERROR: A relationship is being added or deleted from an AssociationSet 'FK_User_ContactInfo'. With cardinality constraints, a corresponding 'User' must also be added or deleted. 

//     this.ContactInfoReference.Value = null; 
        // ERROR : A relationship is being added or deleted from an AssociationSet 'FK_User_ContactInfo'. With cardinality constraints, a corresponding 'User' must also be added or deleted. 
//     this.ContactInfoReference.EntityKey = 
//      new EntityKey (
//       "ProjectRedEntities.ContactInfo", "ContactInfoID", null); 
        // ERROR: Value cannot be null. 

//     this.ContactInfoReference.EntityKey = EntityKey.NoEntitySetKey; 
        // ERROR: The EntityKey property cannot be set to EntityNotValidKey, NoEntitySetKey, or a temporary key. 

//     this.ContactInfoReference.EntityKey = 
//      new EntityKey(); 
        // ERROR The EntityKey property cannot be set to EntityNotValidKey, NoEntitySetKey, or a temporary key. 
       } 
       //To notify others listenging that the property has changed 
       this.OnPropertyChanged ("ContactInfoReference_ContactInfoID"); 
      } 
     } 

나는 사실의 관계에 널 (NULL) 필드를 설정하려고하면

는 I 형 CONTACTINFO이 유효하지 않습니다에 대한

가 'ContactInfoID'컴파일 오류를 얻을. 키의 모든 부분은 Null이 허용되지 않아야합니다.

null ContactInfoID가있는 데이터베이스에서 사용자를로드 할 수 있지만 EntityKey를 null로 설정하려고 할 때 예외가 발생합니다.

어떻게 User.ContactInfoID를 null로 설정합니까?

답변

0

User.ContactID를 -1로 설정하여 누락 된 값을 나타낼 수 없습니다. 엔티티 키를 mucking하기보다는 null을 사용하여 이것을 수행 할 수 있습니까? EF는 그 지정으로 -1을 사용하도록 제작되지 않았습니다 ...

+0

예, 엔티티 외부의 null 값을 나타 내기 위해 -1을 사용하고 있습니다. 집합을 살펴보면 값에 대한 분기가 존재합니다! = -1, 그 키가 -1 인 경우 관계를 null로 설정합니다. user.ContactInfo_ContactInfo = -1로 설정하려고하면 EntityKey를 제대로 처리하여 db의 ContactInfo를 null로 설정해야합니다. 어떻게해야합니까? 나는 그들이 내놓은 에러 메시지로 시도한 모든 다른 방법들을 나열했다. – user877179

+0

이 시점에서 새 개체를 만드시겠습니까? 새로운 객체를 만들 때 savechanges가 객체를 커밋하고 컨텍스트를 인식 할 때까지는 엔티티 키를 설정해서는 안됩니다. –

관련 문제