Search for uncommitted data

1

I was doing tests regarding transactions and I saw that if I conduct a search of the registered and uncommitted data they are returned. In the entity framework this does not happen, and from what I researched behind the scenes he works with transactions.

Someone knows how I can get around this:

var conn = new SqlConnection(ConfigurationManager.
                ConnectionStrings["connNorthwind"].ConnectionString);

        conn.Open();
        var transaction = conn.BeginTransaction();

        string productName = "tEST1";


        var commad1 = conn.CreateCommand();
        commad1.Transaction = transaction;

        var commad2 = conn.CreateCommand();
        commad2.Transaction = transaction;

        var commad3 = conn.CreateCommand();
        commad3.Transaction = transaction; 


        commad1.CommandText = "INSERT products(ProductName, Discontinued)VALUES ('" + productName + "', 'true')";
        commad1.ExecuteNonQuery();

        commad2.CommandText = "INSERT products(ProductName, Discontinued)VALUES ('" + productName + "', 'true')";
        commad2.ExecuteNonQuery();

        commad3.CommandText = "SELECT * FROM products where ProductName like '%" + productName + "%'";
        var reader = commad3.ExecuteReader(System.Data.CommandBehavior.SingleResult);

        bool produtoExistente = false;
        if (reader.HasRows)
        {
            produtoExistente = true;
        }


        Assert.IsFalse(produtoExistente);
    
asked by anonymous 13.12.2014 / 00:41

1 answer

0

The data appears because you possibly use the same connection that opened the transaction to query, so it is correct that they appear.

In the Entity Framework, the transaction is not per connection, but per scope . This is because it is the Entity Framework that controls the connection (s), not the programmer. In addition, the transactional scope using MSDTC allows the use of distributed transactions, ideal for load balancing on large systems.

In the case of your code, you still had to perform a Commit " or a Rollback .

    
12.05.2015 / 23:00