0

我有一个LINQ到SQL表/类使用GUID作为主键和外键。 将以前的一些代码因式分解时出现此问题。LINQ到SQl更新主键字段?

生成的异常是

“操作无效由于对象的当前状态”。

我使用的过程是这样:

aspnet_User user() = new aspnet_User(); 
aspnet_user.childTable = new childTable(); 
.. set some properties 
user.Insert() -> my custom method 

... // @我的自定义方法

using (mycontext dc = new context()) 
{ 
user.childTable.ID = (Guid)myNewlyCreatedGuid; 
} 

的赋值childTable.set_UserId()发生异常。

+0

这就是抛出异常: \t \t如果((this._UserId =值)!) \t \t \t { \t \t \t \t如果(this._aspnet_User.HasLoadedOrAssignedValue) \t \t \t \t { \t \t \t \t \t throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); \t \t \t \t} – 2010-04-05 19:19:15

+0

让我补充一点,这也是一个新创建的参考。所以,我无法从数据库加载对象。 – 2010-04-05 19:22:18

回答

1

只要你的模型正确定义的外键关系用户和子表,您不必手动设置子表的主键。我掩盖了为什么在接受的答案this question