c# - Linq problem with inserting new rows that have references to existing records -


(I believe this is the same problem, but there is no answer, and I think I I can better express this problem ...)

I have two Linq-to-SQL classes, state and county Where is the county from Fic to state . Here are some test codes:

  state s = State.GetState ("NY"); // Here I load a state class through Linq DataContext County C = new county (); C.Name = "Rockland"; C.State = s; MyDataContext.GetTable & LT; County & gt; () InsertOnSubmit (c). MyDataContext.SubmitChanges (); // is an exception  

exception is thrown "infringement of primary key constraints 'pk_state'. The object can not include duplicate key in 'dbo.State' .

In other words, what seems to be happening here is that even when my s is loaded as an existing record, when I go to c Attempt to include, Linq assumes that all related items, including the state , are also required to be included!

It is completely absurd, and I can not believe that Microsoft will make such a big mistake -

< P>

what state. Using the gatestate (...) function, MyDataContext.GetTable & LT of the same datacontext; County & gt; () ?


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -