Selecting row in SSMS causes Entity Framework 4 to Fail

Posted by Eric J. on Stack Overflow See other posts from Stack Overflow or by Eric J.
Published on 2010-06-03T03:56:55Z Indexed on 2010/06/03 4:04 UTC
Read the original article Hit count: 193

Filed under:

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();
}

© Stack Overflow or respective owner

Related posts about entity-framework-4