views:

27

answers:

0

I have a simple Entity Framework 4 unit test that creates a new record, saves it, attempts to find it, then deletes it. All works great, unless...

... I open up SQL Server Management Studio while stopped at a breakpoint in the unit test and execute a SELECT statement that returns the row I just created (not SELECT FOR UPDATE, not WITH (updlock), no transaction, just a plain SELECT).

If I do that before attempting to find the row I just created, I don't find the row. If I instead do that after finding the row but before deleting the row, I do find the row but get an OptimisticConcurrencyException.

This is consistently repeatable.

Unit Test:

[TestMethod()]
public void CreateFindDeleteActiveParticipantsTest()
{
    // Setup this test
    Participant utPart = CreateUTParticipant();

    ctx.Participants.AddObject(utPart);
    ctx.SaveChanges();

    // External SELECT Point #1:
    // part is null

    // Find participant
    Participant part = ParticipantRepository.Find(UT_SURVEY_ID, UT_TOKEN);
    Assert.IsNotNull(part, "Expected to find a participant");

    // External SELECT Point #2: 
    // SaveChanges throws OptimisticConcurrencyException

    // Cleanup this test
    ctx.Participants.DeleteObject(utPart);
    ctx.SaveChanges();
}

related questions