locked
use transactions with multiple functionscall RRS feed

  • Question

  • i use EF to write data to the database

    I have 2 table /classes in my model table program  and table test.

    every program can have one or more tests

    i want to write  data to a program and his tests in transaction. when error is occured by write program or teststep. i want to rollback the inserts. either describe both tables or none

    I habe in my c#-Dll two functions:

    int InsertProgram(string name, string index)

    {

    try{

    using (Entities context = new Entities(conn))

    {

    ...

    context.addObject

    context.savechanges

    ..

    return ID

    }  // end using

    }

    catch(){}

    }

    int InsertTest(program_ID, short number, string name, double min, double max, string unit)

    try{

    using (Entities context = new E$ntities(conn))

    {

    ...

    context.addObject

    context.savechanges

    ..

    return ID

    }

    }

    catch(){}

    }

    i want call the functions from a another application

    myEFdll->beginstorage -> begin transaction

    myEFdll->insert program

    myEFdll->insert test1

    myEFdll->insert test2

    ......

    myEFdll->insert test n

    myEFdll->endstorage -> end transaction

    how can i use transaction in my dll in my function?

    Friday, July 27, 2012 6:11 AM

Answers

  • Hi Sibourg,

    Thank you for your post.

    If you want to avoid creating an instance of the context in every function, please create the instance of the context in advance and pass it to every function as a parameter.

    Second, you can find a nice transaction example on this page: http://msdn.microsoft.com/en-us/library/bb738523.aspx

    In this example, there is a Boolean variable which acts as a flag that if the transaction complete without any error. The “SaveOptions” specifies the behavior of the object context when the SaveChanges(SaveOptions) method is called: http://msdn.microsoft.com/en-us/library/system.data.objects.saveoptions.aspx

    If the SaveChanges method was called and the AcceptAllChangesAfterSave was not specified, the user must call the AcceptAllChanges method. The AcceptAllChanges method is useful in the scenario where a transaction has failed and a user wants to retry.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Friday, August 3, 2012 2:31 AM
    Monday, July 30, 2012 6:08 AM

All replies

  • i use EF to write data to the database.
    i habe in model two tables program and test. a program can i have one or more tests

    the implementation of the db-functions for insert are in a c#-dll this function are called from another application.

    the c#-dll

    public long insertTest(string name, string index)
    {
    long id=0;
                try
                {
                    using (TestEntities context = new TestEntities(conn))
                    {
                        Test test = new Test
                        {
                            Name = name,
                            Index = index
                        };

                        
                        context.Tests.AddObject(test);
                        context.SaveChanges();
                        id = test.ID;
                    }

                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                return id; //needed for storing tests
            }
            
       
       public long InsertTestStep(int test_ID, short nr, string name, double lowerLimit, double upperLimit, string unit)
               {
                   try
                   {
                       using (TestEntities context = new TestEntities(conn))
                       {
                           TestStep testStep = new TestStep
                           {
                               TEST_ID = test_ID,
                               INDEXNO = nr,
                               NAME = name,
                               LOWERLIMIT = (decimal)lowerLimit,
                               UPPERLIMIT = (decimal)upperLimit,
                               UNIT = unit
                           };
       
                          
                           context.TestSteps.AddObject(testStep);
                           context.SaveChanges();
                       }
                   }
                   catch (System.Exception ex)
                   {
                       MessageBox.Show(ex.ToString());
                   }
                   return 0;
               }
        }
        
        the main application call the dll-functions
        
        MyEfDll.Beginstorage // begin transaction
        MyEfDll.InsertTest
        MyEfDll.Insert testStep() // insert test step 1
        MyEfDll.Insert testStep()  // insert test step 2
        MyEfDll.Insert testStep()  // insert test step 3
        ......
        MyEfDll.Insert testStep()  // insert last test step
        MyEfDll.Endtransaction
        
        in every function a create new instance of the context and all documenation i found about transaction are used wit ohne contect or connection
        how can i use transactions in my dll. i want that that either data are written to both tables or none if a error ocured

       
    • Merged by Alexander Sun Monday, July 30, 2012 5:43 AM Duplicate
    Friday, July 27, 2012 6:34 AM
  • i use EF to write data to the database.
    i habe in model two tables program and test. a program can i have one or more tests

    the implementation of the db-functions for insert are in a c#-dll this function are called from another application.

    the c#-dll
    public long insertTest(string name, string index)
    {
    long id=0;
                try
                {
                    using (TestEntities context = new TestEntities(conn))
                    {
                        Test test = new Test
                        {
                            Name = name,
                            Index = index
                        };
    
                        
                        context.Tests.AddObject(test);
                        context.SaveChanges();
                        id = test.ID;
                    }
    
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                return id; //needed for storing tests
            }
            
       
       public long InsertTestStep(int test_ID, short nr, string name, double lowerLimit, double upperLimit, string unit)
               {
                   try
                   {
                       using (TestEntities context = new TestEntities(conn))
                       {
                           TestStep testStep = new TestStep
                           {
                               TEST_ID = test_ID,
                               INDEXNO = nr,
                               NAME = name,
                               LOWERLIMIT = (decimal)lowerLimit,
                               UPPERLIMIT = (decimal)upperLimit,
                               UNIT = unit
                           };
       
                          
                           context.TestSteps.AddObject(testStep);
                           context.SaveChanges();
                       }
                   }
                   catch (System.Exception ex)
                   {
                       MessageBox.Show(ex.ToString());
                   }
                   return 0;
               }
        }

      
        the main application call the dll-functions
        
        MyEfDll.Beginstorage // begin transaction
        MyEfDll.InsertTest
        MyEfDll.Insert testStep() // insert test step 1
        MyEfDll.Insert testStep()  // insert test step 2
        MyEfDll.Insert testStep()  // insert test step 3
        ......
        MyEfDll.Insert testStep()  // insert last test step
        MyEfDll.Endtransaction
        
        in every function a create new instance of the context and all documenation i found about transaction are used wit ohne contect or connection
        how can i use transactions in my dll. i want that that either data are written to both tables or none if a error ocured

       
    • Merged by Alexander Sun Monday, July 30, 2012 6:49 AM Duplicate
    Friday, July 27, 2012 10:29 AM
  • Hi Sibourg,

    Thank you for your post.

    If you want to avoid creating an instance of the context in every function, please create the instance of the context in advance and pass it to every function as a parameter.

    Second, you can find a nice transaction example on this page: http://msdn.microsoft.com/en-us/library/bb738523.aspx

    In this example, there is a Boolean variable which acts as a flag that if the transaction complete without any error. The “SaveOptions” specifies the behavior of the object context when the SaveChanges(SaveOptions) method is called: http://msdn.microsoft.com/en-us/library/system.data.objects.saveoptions.aspx

    If the SaveChanges method was called and the AcceptAllChangesAfterSave was not specified, the user must call the AcceptAllChanges method. The AcceptAllChanges method is useful in the scenario where a transaction has failed and a user wants to retry.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Friday, August 3, 2012 2:31 AM
    Monday, July 30, 2012 6:08 AM