efcore: Database.BeginTransaction() throwing on InMemory Repository
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
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 23 (11 by maintainers)
I think the exception message tells you everything you need to know 😄…
So here is what your configuration code would look like…
@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
@rowanmiller I am a little confused about this issue. The inmemorydb is that now mainly for testing? ms testing in-memory
But if we cannot make transactions, how can we use it for tessting e.g. our repositories that accept a transactions.
Thanks for your time
@generik0 It depends what you are trying to test. If the code is largely database agnostic and just needs to get data from somewhere, then in-memory is a good choice. If the code depends on relational concepts, like transactions, but is largely agnostic to the specific relational database engine, then SQLite in-memory might be better. If the code depends on specific functionality of the database engine, then the only option is to test with that specific engine.