1

我正在尝试使用 ADO.NET 数据服务来更新我的数据库。

表:

  • 人(PK PersonId;FK EntityRootId)
  • EntityRoot(PK EntityRootId,FK EntityTypeId)
  • 实体类型(PK EntityTypeId)

我正在尝试创建一个 EntityRoot(不幸的名称,因为它可能与 Entity Framework 的 Entity 混淆。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.EntityTypeId 字段的引用?

感谢您对此的任何见解。

4

2 回答 2

4

我认为这样做时您应该使用 SetLink 而不是 AddLink,因为您指示的属性是从 Entity 到 EntityType(一个 Entity 有一个 EntityType)。

这基本上是错误消息告诉您的内容(AddLink 仅适用于 Collection 属性,例如从类型到实体的反向属性,或多对多关系)。

于 2009-03-17T15:11:59.140 回答
0

这只是您的问题中的错字还是应该是

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

在你的问题开始时,你写“EntityTypes”(以“s”结尾)。

于 2009-03-17T15:07:21.617 回答