我已经成功地使用EF4使用服务器生成的GUID自动插入行:
http://leedumond.com/blog/using-a-guid-as-an-entitykey-in-entity-framework-4/
现在,如果在RowID (guid)和ParentRowID (guid)之间存在主外键约束,该如何执行这项任务?我应该将.ParentRowID设置为什么?
NewCut = New Row With
{
.ParentRowID = .RowID
}SaveChanges每次都会大发雷霆。
主键是GUID这一事实实际上是无关紧要的,因为我使用一个标准的自动生成的整数尝试了相同的测试,但没有成功。
发布于 2010-08-05 18:29:40
解决方案和你发布的一样简单。
只需在代码中创建父实体和子实体,不要设置实体键,也不要忘记为所有服务器生成的Guid列设置StoreGeneratedPattern。
然后执行MasterEntityInstance.Children.Add(ChildEntityInstance),或ChildEntityInstance.MasterEntity = MasTerEntityInstance并调用SaveChanges。
在SaveChanges调用之后,两个guid属性都将填充正确的GUID值,而ChildEntity.MasterEntity实体键将填充必要的MasterEntity实体键值。
https://stackoverflow.com/questions/3409777
复制相似问题