我正在尝试使用 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 字段的引用?
感谢您对此的任何见解。