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

Multiple DbContext Demands (Redundant?) Migration Parameter #5996

Closed
robbpriestley opened this issue Jul 6, 2016 · 8 comments
Closed

Multiple DbContext Demands (Redundant?) Migration Parameter #5996

robbpriestley opened this issue Jul 6, 2016 · 8 comments

Comments

@robbpriestley
Copy link

robbpriestley commented Jul 6, 2016

Steps to reproduce

  • Create a project
  • Add a DbContext and associate some models with it
  • Then add another DbContext and associate some other models with it
  • (There are now two contexts co-existing in the project. I do this if one of the contexts becomes invalidated I can still write log entries through the other)
  • dotnet ef migrations add initial

The issue

Error message:

More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

Discussion: While I can understand the conceptual logic for this, specifying one context or the other doesn't actually restrict the migration to only those models associated with the specified context. Thus the need to specify context seems redundant and unnecessary.

Example: If models A and B are associated with context 1 and model C is associated with context 2 and I create a migration and specify context 1, model C is migrated regardless. This either seems like a bug, a logical inconsistency with the design, or is based on presumptions which I am unaware of.

Further technical details

EF Core version: 1.0.0-rc2-final
Operating system: OSX El Cap
Visual Studio version: VS Code 1.2.1
Database: Postgres
"Npgsql": "3.1.2",
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0-rc2-release1"

@rowanmiller
Copy link
Contributor

Migrations works by migrating the model/schema associated with a given context. At this stage we don't really have support for models that overlap between contexts - though we do have #2725 on the backlog to improve this.

@rowanmiller
Copy link
Contributor

Someone just pointed out that you had said A & B belong to Context 1 and C belongs to Context 2. If this is the case, then changes to C should not get included in migrations for Context 1. However, if there are navigation properties between A/B & C then C is actually part of the model for Context 1, even if it doesn't have a DbSet property exposed.

You could exclude C from the model in Context 1 if it's not supposed to be part of the model https://docs.efproject.net/en/latest/modeling/included-types.html.

@robbpriestley
Copy link
Author

robbpriestley commented Jul 6, 2016

Thanks, but just to be clear, my models don't actually overlap. At least, I'm pretty sure they don't. I would have expected that any models associated with a context would be migrated in isolation. However my observation is different. Despite specifying the context, models from the other context are migrated also. However, as you also point out there could be other properties at work...

@rowanmiller
Copy link
Contributor

Any chance you could share the model? Your expectation is correct.

@robbpriestley
Copy link
Author

@rowanmiller I apologize for the delay in answering. The code in question is proprietary and I'm reluctant to publish or share it. I realize this is not useful to you. I feel like I should offer to mock up a code base and attempt to re-create the issue, but I have some current time constraints.

So perhaps for now this issue is just in the early information-gathering stage and you may later be able to corroborate and/or re-create. In the meantime, this issue is not a showstopper and you may feel free to close the ticket or do as you think best. Many thanks to you and the team for the work you do.

@rowanmiller
Copy link
Contributor

@robbpriestley I've done some testing around two separate models in the same project and they did not interfere with each others migrations. My speculation is that there is something joining the classes from each model, so that all the entities are ending up in both models, but of course that is just a guess from limited information.

I am going to close this out as I don't think there is anything actionable from our side, but if you do end up with time to distill something that can be shared then just comment and we will re-open.

BTW you could use DbContext.Model.GetEntityTypes() to quickly check which entities are ending up in the model for a given context.

@robbpriestley
Copy link
Author

@rowanmiller you were completely right. I'm very sorry for wasting your time. But not completely because meanwhile I learned something important. It turned out I was referencing a model entity in both of the contexts!! My bad but I am so happy to have this figured out, thanks!!

@irejwanul
Copy link

I had problem of EF core Migrations, when I tried to apply migrations for one Context ( i.e. In My Project I have more than one Context), then other Context also included in the Migration and prevented with error for Primary of the Models included in another Context. Not included in the Current Context (Migration to be Applied). The real bug was foreign key relation to one Entity of the other Context was in one of the Entity of the Migration to be applied Context. When I remove the relation then the error gone and the migrations completed.

@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants