question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Database.BeginTransaction() throwing on InMemory Repository

See original GitHub issue

I have a working code for EF7 beta6 with the SQL CE 4.0 data-provider, which uses some transactions inside.

Now I want to unit-test these things. However, when I switch the context options to be in-memory, I get the following exception: “No service for type ‘Microsoft.Data.Entity.Storage.IRelationalConnection’ has been registered.” Stack: at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.Data.Entity.Infrastructure.AccessorExtensions.GetService[TService](IAccessor`1 accessor) at Microsoft.Data.Entity.RelationalDatabaseFacadeExtensions.GetRelationalConnection(DatabaseFacade databaseFacade) at Microsoft.Data.Entity.RelationalDatabaseFacadeExtensions.BeginTransaction(DatabaseFacade databaseFacade)

Basically, the code to reproduce:

var options = new DbContextOptionsBuilder<BloggingContext>();
options.UseInMemoryDatabase(persist: true);
//  options.UseSqlCe(@"Data Source=Blogging.sdf"); --> works fine

using (var x = new BloggingContext(entityOptions))
{
    var tx = x.Database.BeginTransaction(); // Fails, when using InMemoryDatabase
    x.Blogs.Add(new Blog() {Url = "URL-" + (i*10)});
   ...
}

What am I doing wrong? Am I missing some context-options?

I would have thought, that these things work out of the box.

Cheers, Harald

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

41reactions
rowanmillercommented, Feb 2, 2017

I think the exception message tells you everything you need to know 😄…

Warning as error exception for warning ‘InMemoryEventId.TransactionIgnoredWarning’: Transactions are not supported by the in-memory store. See http://go.microsoft.com/fwlink/?LinkId=800142. To suppress this Exception use the DbContextOptionsBuilder.ConfigureWarnings API. ConfigureWarnings can be used when overriding the DbContext.OnConfiguring method or using AddDbContext on the application service provider.

So here is what your configuration code would look like…

options
    .UseInMemoryDatabase()
    .ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning))
4reactions
kimwandevcommented, Feb 3, 2017

@rowanmiller Thank you so much for pointing that out. I have tested it and it worked like a charm. I am going to my eye doctor after my shift and get a pair of glasses so that I can read the exceptions carefully next time 😄

Again, Thanks for your help!

Regards, Kimwan Ogot

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Mocking Database transactions?
But if I try to mock BeginTransaction(), I get initialization errors: NotSupportedException: "Invalid setup on a non-virtual member: m => m.
Read more >
Avoid In-Memory Databases for Tests
The problem is, just like an integration test that uses a single transaction, is that real-life behavior is much different and more complex,...
Read more >
Using Transactions - EF Core
While all relational database providers support transactions, other providers types may throw or no-op when transaction APIs are called.
Read more >
Testing without your Production Database System - EF Core
Note that by default, if a transaction is started, the in-memory provider will throw an exception since transactions aren't supported. You may ...
Read more >
Entity Framework Core - Isolation Of Integration Tests
Given is a DemoRepository with a method AddProduct that we want to ... method must not starting transactions using BeginTransaction() or it ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found