Question

I'm trying to use linq to sql for integration testing of stored procedures. I'm trying to call an updating stored procedure and after that retrieving the updated row from db to verify the change. All this should happen in one transaction so that I can rollback the transaction after the verification.

The code fails in assert, because the the row I retrieved does not seem to be updated. I know that my SP works when called from ordinary code. Is it even possible see the updated row in same transaction?

I'm using Sql Server 2008 and used sqlmetal.exe to create linq-to-sql mapping.

I've tried many different things, and right now my code looks following:

        DbTransaction transaction = null;
        try
        {
            var context =
                new DbConnection(
                    ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
            context.Connection.Open();
            transaction = context.Connection.BeginTransaction();
            context.Transaction = transaction;
            const string newUserName= "TestUserName";
            context.SpUpdateUserName(136049 , newUserName);
            context.SubmitChanges();
            // select to verify
            var user=
                (from d in context.Users where d.NUserId == 136049 select d).First();
            Assert.IsTrue(user.UserName == newUserName);
        }
        finally
        {
            if (transaction != null) transaction.Rollback();
        }
Was it helpful?

Solution

I believe you are coming acress a stale datacontext issue.

Your update is done through a stored procedure so your context does not "see" the changes and has no way to update the Users.

If you use a new datacontext to do the assert, it usually works well. However, since you are using a transaction you probably have to add the second datacontext to the same transaction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top