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

Allow named in-memory stores #3253

Closed
ajcvickers opened this issue Sep 28, 2015 · 11 comments
Closed

Allow named in-memory stores #3253

ajcvickers opened this issue Sep 28, 2015 · 11 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@ajcvickers
Copy link
Member

We decided in API review to remove the "persist" flag from the in-memory provider and replace with a string that could be used to create a named in-memory store. However, we were pretty much never using the persist: false anyway, so I just removed it and created a bug to triage adding named databases so we could triage appropriately.

@rowanmiller
Copy link
Contributor

At the moment, do you get one per ServiceProvider, or one per context type per ServiceProvider? Either way, I don't think we need the named database capability for RC.

@ajcvickers
Copy link
Member Author

You get one table per entity type per service provider.

@rowanmiller
Copy link
Contributor

Reactivating (presumably for the backlog) as I am writing the docs for in-memory testing and this is not very easy at the moment. I think named instances could be a good solution to avoid folks having to muck around with ServiceCollections.

@rowanmiller rowanmiller added this to the 1.0.0 milestone Feb 1, 2016
ajcvickers added a commit that referenced this issue Apr 15, 2016
Issue #3253

Makes it easier to write isolated tests while still sharing a service provider between tests.
ajcvickers added a commit that referenced this issue Apr 19, 2016
Issue #3253

Makes it easier to write isolated tests while still sharing a service provider between tests.
ajcvickers added a commit that referenced this issue Apr 19, 2016
Issue #3253

Makes it easier to write isolated tests while still sharing a service provider between tests.
@NeilCross
Copy link

Sorry for bringing up an old thread, but currently the Testing with InMemory in the EF docs:
mentions requiring a workaround to this issue.

Caution
Currently you need to use ServiceCollection and IServiceProvider to control the scope of the InMemory database, which adds complexity to your tests. In the next release after RC2, there will be improvements to make this easier, see issue #3253 for more details.

Now 1.0.0 is released and I assume this warning in the docs not required anymore, and a ServiceCollection is no longer required to instantiate an in-memory context
Can you confirm this for me?

I'm assuming the simplest way to instantiate an in-memory context now is:

var builder = new DbContextOptionsBuilder();
builder.UseInMemoryDatabase();
var context = new BloggingContext(builder.options)

@rowanmiller
Copy link
Contributor

@NeilCross opened dotnet/EntityFramework.Docs#287 to fix the docs. The docs are currently migrating to a new system, so I'll fix once the migration is done.

@NeilCross
Copy link

Good to know, thanks Rowan.

@logamanig
Copy link

Following code works perfectly for me,
// Requires separate DI Service provider to isolate db instance as InMemory database is also shared within
// the class scope since the App Service Factory is configured to per scope instance for dbcontext
var internalServiceProviderForDbContext = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
var options = new DbContextOptionsBuilder()
.UseInternalServiceProvider(internalServiceProviderForDbContext)
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;

@ajcvickers
Copy link
Member Author

@logamanig It's generally best to avoid calling UseInternalServiceProvider if you don't really need to.

@logamanig
Copy link

logamanig commented May 31, 2019

@logamanig It's generally best to avoid calling UseInternalServiceProvider if you don't really need to.

without calling UseInternalServiceProvider, not able to isolate the InMemoryDatabase instance between test cases. Just a unique database name is not enough to isolate.

@ajcvickers
Copy link
Member Author

@logamanig There are other ways to deal with that that aren't such a heavy hammer:

  • Figure out why the internal service provider is being built differently for different uses of the context--look at the logging
  • Specify the database root in UseInMemoryDatabase

Also, in 3.0 is is less likely you will run into this case at all.

@logamanig
Copy link

Thanks @ajcvickers !
as you suggested, specifying the database root avoids internal service provider heavy lifting..
here's the sample code,

var options = new DbContextOptionsBuilder<DbContextClass>()
                .UseInMemoryDatabase(
                    databaseName: Guid.NewGuid().ToString(),
                    databaseRoot: new InMemoryDatabaseRoot()
                    )
                .Options;

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

4 participants