none
EF maintain transaction automatically or not RRS feed

  • Question

  • when we try to enter data in multiple table by ado.net then we need to work with transaction scope class but if we need to enter data in multiple table by EF then what we need to use to have transaction there means for any reason if i get error when we insert data in second table then data inserted in 1st table should be roll backed. so tell me how to achieve this in EF?

    see this code

    private void AddProducts(double salePrice, int taxValue)
    {
        using (var ctx = new Entity())
        {
    
            Product prodObject = new Product
            {
                PId = //NEW ID,
                SalePrice = salePrice
            };
    
            Product_tax pTax = new Product_tax 
            {
                pid = prodObject.PId,
                taxid = taxValue
            };
    
            ctx.product.AddObject(prodObject);
            ctx.Product_tax .AddObject(pTax);
    
            ctx.SaveChanges();
        }
    }

    if data insert into product table but encounter error when we EF insert data into Product_tax then what will happen ?

    the data which has been inserted in product table will be roll backed or not?

    transaction will be maintain automatically or not?

    please guide me.

    Tuesday, November 7, 2017 1:11 PM

Answers

  • if data insert into product table but encounter error when we EF insert data into Product_tax then what will happen ?

    All data in both tables will be rolled back.

    the data which has been inserted in product table will be roll backed or not?

    It will be rolled back.

    transaction will be maintain automatically or not?

    You can test this yourself by adding more than one child object and setting a condition for a child object with data to make it blow up,  like trying to save null data to a column that will not accept a null value or make SQL Server throw an exception of string data being saved for a string column is too big and truncation of the data occurs, as examples of how you can get an exception to occur on the saving of data to test transactions.

    • Marked as answer by Sudip_inn Tuesday, November 7, 2017 9:06 PM
    Tuesday, November 7, 2017 6:10 PM

All replies

  • if data insert into product table but encounter error when we EF insert data into Product_tax then what will happen ?

    All data in both tables will be rolled back.

    the data which has been inserted in product table will be roll backed or not?

    It will be rolled back.

    transaction will be maintain automatically or not?

    You can test this yourself by adding more than one child object and setting a condition for a child object with data to make it blow up,  like trying to save null data to a column that will not accept a null value or make SQL Server throw an exception of string data being saved for a string column is too big and truncation of the data occurs, as examples of how you can get an exception to occur on the saving of data to test transactions.

    • Marked as answer by Sudip_inn Tuesday, November 7, 2017 9:06 PM
    Tuesday, November 7, 2017 6:10 PM
  • if you know then please tell me transaction will be maintain automatically or not ?

    i rarely work with EF. so looking for your answer.

    Wednesday, November 8, 2017 10:04 AM