c# - Insert into multiple tables using WCF Transactions -


i trying add entry table , use primary key of added entry create additional entry table.

the error getting is

the transaction manager has disabled support remote/network transactions. (exception hresult: 0x8004d024)

i believe caused creating multiple connections within single transactionscope, doing within 1 context / using statement, not believe should receiving error.

service

    [operationbehavior(transactionscoperequired = true)]     public void creategroup(newgroupdata data)     {         var grouprepo = _grouprepo ?? new investigatorgrouprepository();         grouprepo.creategroup(data.userid, data.investigatorgroupname, data.hasgameassignment, data.institutionid);      } 

repository

   public void creategroup(string userid, string investigatorgroupname, bool hasgameassignment, int institutionid)     {         using (var context = new gamedbcontext())         {             var newgroup = new investigatorgroup()             {                 investigatorgroupname = investigatorgroupname,                 hasgameassignment = hasgameassignment,                 institutionid = institutionid,                 istrashed = false             };              int institutionuserid =                 context.institutionusers.where(                     iu => !iu.istrashed && iu.apuser.userid == userid && iu.institutionid == institutionid).select(iu => iu.institutionuserid).single();              var newgroupuser = new investigatorgroupuser()             {                 institutionuserid = institutionuserid,                 investigatorgroup = newgroup,                 creationdate = datetime.now             };             context.investigatorgroupusers.add(newgroupuser);              context.savechanges();         }     } 

you start wrong assumption.

the line...

int newgroupid = context.investigatorgroups.add(newgroup).investigatorgroupid; 

...will assign 0 newgroupid. add method marks entity insert, doesn't insert it. savechanges writes data database, not other method in entity framework.

so assignment...

investigatorgroupid = newgroupid, 

...is faulty well. have assign new investigatorgroup navigation property in investigatorgroupuser:

investigatorgroup = newgroup, 

add navigation property investigatorgroupuser if haven't got yet.

if have that, it's enough execute these lines:

context.investigatorgroupusers.add(newgroupuser); context.savechanges(); 

no need add newgroup object too, added adding newgroupuser.

so if that, transaction need 1 savechanges uses internally default. code show, don't need transactionscope. if part of greater wcf transaction story may different, think @ least needed misconceptions straightened out.


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? -