문제

ADO.NET 데이터 서비스를 사용하여 데이터베이스를 업데이트하려고합니다.

테이블 :

  • 사람 (pk personid; fk entityrootid)
  • EntityRoot (PK EntityRootID, FK EntityTypeid)
  • EntityTypes (PK EntityTypeid)

EntityRoot (EntityRoot의 엔티티와 혼동 될 수 있으므로 불행한 이름입니다. EntityRoot가 내 도메인에 있습니다) 데이터베이스에 추가하려고합니다.

var entRoot = EntityRoot.CreateEntityRoot(
    0, "Lou", DateTime.Now, "Lou", DateTime.Now);
var entityType = 
   (from type in myContext.EntityTypeSet
    where (type.Description == "Person")
    select type).FirstOrDefault(); // this works fine and returns the entityType I’m looking for

entRoot.EntityType = entityType;
myContext.AddToEntityRootSet(entRoot); 

// with the line below I get a runtime error:
//  “AddLink and DeleteLink methods only work when the sourceProperty is a collection.”
//myContext.AddLink(entRoot, "EntityType", entityType); 

// without the line above I get an error from the save:
//  “Entities in 'MyEntities.EntityRootSet' participate in the 'FK_EntityRoots_EntityTypeId_Lookup_EntityTypes' 
//    relationship. 0 related 'EntityTypes' were found. 1 'EntityTypes' is expected.”
myContext.BeginSaveChanges(SaveChangesOptions.Batch,
                      new AsyncCallback(OnSaveAllComplete),
                      null); 

Entroot.entityPeid 필드에 대한 참조를 추가하려면 어떻게해야합니까?

이것에 대한 통찰력에 감사드립니다.

도움이 되었습니까?

해결책

이 작업을 수행 할 때 ADDLINK 대신 SetLink를 사용해야한다고 생각합니다. 귀하가 나타내는 속성은 엔티티에서 EntityType (엔티티에는 하나의 EntityType가 있음)이기 때문입니다.

이것은 기본적으로 오류 메시지가 말하는 것입니다 (AddLink는 유형에서 엔티티까지 또는 많은 관계에 대한 리버스 속성과 같은 수집 속성에서만 작동합니다).

다른 팁

그것은 당신의 질문에 오타입니까, 아니면

myContext.AddLink(entRoot, "EntityTypes", entityType); 

질문이 시작될 때 "EntityTypes"( "S"와 함께 끝)를 씁니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top