views:

253

answers:

2

I have a linq to sql table/class that uses a GUID as both a primary key and as foreign key. This problem came up when factoring some code that previously worked.

The exception generated is

"Operation is not valid due to the current state of the object."

The process I use is as such:

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

... @ my custom method

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

The exception occurs on the assignment childTable.set_UserId().

+1  A: 

As long as your model correctly defines the foreign key relationship between the User and the child table, you don't have to manually set the primary key for the child table. I cover the reason why in the accepted answer to this question.

Neil T.