c# - How to update record correctly? -


could tell me how solved problem:

i want update record in db:

internal static void releasemachine(int idmachine)         {             machine machine = getbyid(idmachine);             if (machine != null)                 machine.idworker = null;         }  internal static machine getbyid(int idmachine)     {         return (from m in database.current.machines m.idmachine == idmachine select m).firstordefault();     } 

but when try, program throws exception:

system.data.linq.foreignkeyreferencealreadyhasvalueexception ,

operation not valid due current state of object.

thanks help

i'm guessing idworker foreign key, , setting foreign key null isn't allowed foreign key no longer match record in referenced table. create orphaned record in referenced table. have delete worker database first before can remove foreign key.

if try same thing in sql query on database directly won't allow set idworker null either. if allow set idworker null in database directly, may not have setup entities correctly (nullable foreign key in entity setup).


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -