我正在尝试使用ADO.NET数据服务来更新我的数据库。
表:
我正在尝试创建一个EntityRoot(不幸的名称,因为它可能与Entity Framework的实体混淆.IntityRoot在我的域中)并将其添加到数据库中:
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字段的引用?
感谢您对此有任何了解。
答案 0 :(得分:4)
我认为在执行此操作时您应该使用SetLink而不是AddLink,因为您指示的属性是从Entity到EntityType(实体有一个EntityType)。
这基本上是错误消息告诉您的内容(AddLink仅适用于Collection属性,例如从类型到实体的反向属性,或者用于多对多关系)。
答案 1 :(得分:0)
这只是你问题中的拼写错误还是
myContext.AddLink(entRoot, "EntityTypes", entityType);
在你的问题开始时你写“EntityTypes”(带有和结束“s”)。