Linq to sql multiple data context in same transaction -


I am working on a project where I have multiple reposetories to fetch data from differnt tables. I have all my treasures free They create new datacontacts, add rows to the table and apply the change commands. Now if in my service, there is a situation where I have to put data in several tables, but it should be in a transaction. I can get it using TrnasctionScope stuff, but they require data. As I am using StrucutreMap to create my objects, I can not get the same data reference, so my transaction fails.

Here are my objects.

  Interface Iconation {MyDataContext GetContext (); } Public class connection: Iconation {public MyDataContext GetContext (new {{New MyDataContext back}); }} Interface IRepositryA {int SaveDataA (objectA A); } Public Class Repository A: IRAPOSITIA {Public Ink Service (Object AA) {var context = Object factory.Get instance & lt; Iconization & gt; (). GetContext (); Context.TableA.InsertOnSubmit (a); Context.SubmitChanges (); Back a.ID; }} Interface IRepositryB {int SaveDataA (objectB b); } Public Class Repository A: IRAPositry {Public Ink Service B (Object BB) {var context = Object factor. Get Instance & Lt; Iconation & gt; (). GetContext (); Context.TableB.InsertOnSubmit (b); Context.SubmitChanges (); Return b.ID; Now in my service layer, I have called them as the  
  Public MySivaClass () {Public SevenAll (Object AA, Obszeib B) {using (TransactionScope Tran); {ObjectFactory.GetInstance & lt; IRepositryA & gt; (). SaveDataA (A); B.FKID = AIID; ObjectFactory.GetInstance & LT; IRepositryB & gt; () SaveDataB (b). Trn.complete (); }}}  

How would I pass the same data reference for two differnt repositories? I thought that I was creating an overloaded method in each repository, which was given below the IDataContext parameter Accepts

Interface IRAP positories {Int. ServiceData (Object AA); Int. Savata (Iconction Connection, Object AA); } Public Class Repository A: IRPository {Public Ink Service (Object AA) {var connection = ObjectFitcher. Gate instances and lt; Iconation & gt; (); Return Service Data (connection, A); } Public Inch Services (Iconction Connection, Object AA) {var context = connection.GetContext (); Context.TableA.InsertOnSubmit (a); Context.SubmitChanges (); Back a.ID; }} Interface IRepositryB {int SaveDataA (objectB b); Intetetetta (Iconction Connection, Object BB); } Public Class Repository A: IEPository {Public Ink Service B (Object BB) {var connection = Object Factory.Get instance & lt; Iconation & gt; (); Return service data (connection, b); } Public Inch Services (Iconction Connection, Object BB) {var reference = Connection. GateContact (); Context.TableB.InsertOnSubmit (b); Context.SubmitChanges (); Return b.ID; }}

And in my service level, I will implement it in the form of Public MySivaClass () {Public Service (Object A, ObserveBB)

  {Iconction Connection = ObjectFactory.GetInstance & lt; IConnection & gt; (); (TransactionScope trn); {ObjectFactory.GetInstance & lt; IRepositryA & gt; (). SaveDataA (connection, A); B.FKID = AIID; ObjectFactory.GetInstance & LT; IRepositryB & gt; () SaveDataB (connection, b); Trn.complete (); The other thing is that if I call the GetContext method, then in some way I can configure DataContext to return the same connection. Therefore all the treasures will use the same connection. But I think the connection will survive all the time. I want to make this connection available only during the time of this SaveAll method. 

Information about your help will be given. Thanks in advance.

Cheers Permindor

Transaction scope should be used with many datacontaxes However, as soon as there are more than one connection, the transaction is enhanced in an MSDTC / XA / distributed transaction. For that, you need to run MSDTC on both systems, where you run the code and on the database server.

Alternatively, if you make a clear relationship within the transaction, you can avoid the increase in a distributed transaction and pass it to your dataconsets; In this way, transaction scope will increase in a distributed transaction, and will not trust MSDTC ...


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -