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

Is there a way to use an in memory database with an inherited DBContext? #17562

Closed
foz1284 opened this issue Mar 19, 2019 — with docs.microsoft.com · 2 comments
Closed

Comments

Copy link

foz1284 commented Mar 19, 2019

I am finding when using an in memory database and I create data in the base context database it doesnt exist in the inherited context database,

This is a problem as I am using a Base BLL which is using the base context and an inherited BLL using the inherited context.

The following stack overflow question explains the issue but is there any workaround?

https://stackoverflow.com/questions/48437528/multi-context-inmemory-database

"Beside the same database name, the model must also be the same. This means that in case of an own implementation of IModelCacheKeyFactory, it's Create-Method must return "equal" objects."

thanks!


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@foz1284
Copy link
Author

foz1284 commented Mar 20, 2019

I have found that adding InMemoryDatabaseRoot solves the initial problem of not having any data as below:

.UseInMemoryDatabase(databaseName: "DBName", databaseRoot: new InMemoryDatabaseRoot())

Unfortunately when adding a second entity from the inherited class when a first entity was created in the base class gives the following error since the ID of the entity being added is being set to 1 which already exists:

System.InvalidOperationException: 'The instance of entity type 'OperationsSchedule' cannot be tracked because another instance with the same key value for {'OperationsScheduleID'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.'

I have created a cut down sample application here:

https://github.com/foz1284/EntityFrameworkInMemoryInheritanceTest

@ajcvickers ajcvickers transferred this issue from dotnet/EntityFramework.Docs Sep 2, 2019
@ajcvickers
Copy link
Member

Duplicate of #6872

@ajcvickers ajcvickers marked this as a duplicate of #6872 Sep 2, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants