Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read after SaveChanges does not work inside transaction #22

Open
pierreamerica opened this issue Sep 19, 2015 · 4 comments
Open

Read after SaveChanges does not work inside transaction #22

pierreamerica opened this issue Sep 19, 2015 · 4 comments
Labels

Comments

@pierreamerica
Copy link

I love using Effort/NMemory for my testing, but now I ran into a problem:
When I use an explicit transaction (since I want to be able to do a rollback), the results of SaveChanges are not visible until I do a commit:

using (DbContextTransaction transaction = context.Database.BeginTransaction())
{
    // Store a recipe:
    context.Recipes.Add(recipe);
    context.SaveChanges(); // no commit!

    // Now retrieve it:
    int total = context.Recipes.Count(); // result is zero
}

This behavior is different from SQL Server, which makes newly stored rows visible inside the transaction (so the value of total would be 1). My code relies on this to check what has already been stored.

Is there anything I am doing wrong or is this a bug?

@RtypeStudios
Copy link

+1

@Mediel
Copy link

Mediel commented Jan 8, 2018

Hello, I have the same problem. Normal EF working with this situation... Can someone repair it please? 🔢

Thank you

@magnusbakken
Copy link

Since this issue has been known about for a few years and is probably difficult to fix without significant changes to Effort, I wouldn't hold my breath waiting for a fix. As a workaround I've added a flag to my context class, and when the flag is set to true I simply don't use transactions. As long as you're not running multiple operations against the same context at the same time the transactions shouldn't be too important to the tests.

This gist shows one way to "disable" transactions for tests: https://gist.github.com/magnusbakken/671abb7cf43798cffdb45f29bb839fe1

Use using (var transaction = Context.BeginTransaction()) { ... } to start transactions in your production code, and set IsInMemoryTestContext = true when you create contexts in your test code. I test for the same property for some other stuff that Effort doesn't support, such as ExecuteSqlCommand.

@Mediel
Copy link

Mediel commented Jan 8, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

5 participants