【发布时间】:2011-09-22 21:06:46
【问题描述】:
我有两个实体Parent 和Child,在客户端我创建Parent 然后调用context.submitChanges
在服务器端InsertParent(Parent parent) 我做:
InsertParent(Parent parent)
{
Child child = this.ObjectContext.Childs.CreateObject();
parent.child = child;
if ((parent.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(parent, EntityState.Added);
}
else
{
this.ObjectContext.Parents.AddObject(parent);
}
}
现在我有两个问题。
在 if else 之前,Parent.id 为 0,在它之后仍然为 0,但在数据库中填充了它。
另一个是,Child 被保存但Child.ParentId 为 0。
我不明白为什么。
实现这种行为的正确方法是什么?我应该直接在上下文中调用SaveChanges() 吗?
【问题讨论】:
标签: entity-framework wcf-ria-services domainservices