How to trigger Update-Database on a DbContext in a referenced assembly (NuGet package)

c# dbcontext entity-framework entity-framework-6

Question

Using the Package Manager Console I can run a command similar to the following one

Update-Database -ProjectName: My.Project.Data

The command triggers migrations on MyDbContext defined within the project My.Project.Data located in the currently open solution. Good.

Now the project My.Project.Data resides in the same solution with project My.Project.Mvc. The problem is, My.Project.Mvc uses two db contexts. One that we already covered and another one defined within project Core.Project.Data - this one is built as a NuGet package which is then referenced by My.Project.Mvc. The diagram below should make it clear:

MySolution
├┬ My.Project.Mvc
│├─ references: My.Project.Data (in the same solution)
│└─ references: Core.Project.Data (NuGet)
└─ My.Project.Data [MyDbContext]

CoreSolution
└─ Core.Project.Data [CoreDbContext]

The question is, how can I trigger the migrations for the CoreDbContext from the Package Manager Console of MySolution. If I try to run Update-Database -ProjectName: Core.Project.Data I get the error:

Get-Project : Project 'Core.Project.Data' is not found.

Update: Using the migrate.exe instead is not an option.

1
1
7/19/2016 12:14:54 PM

Accepted Answer

From the document update-database does not support dll.

You can execute EF migration tool from commandline, or add it in VS menu for conveniences.

migrate.exe Core.Project.Data.dll /startupConfigurationFile=Core.Project.Data.dll.config

or change /startupConfigurationFile to /connectionString=your-connection-string

1
7/19/2016 9:37:27 AM

Popular Answer

When migrations are first enabled by Enable-Migration, a Configuration.cs file will be added to the project. If this file exists for Core.Project.Data, tell Entity Framework to use this config:

Update-Database -ConfigurationTypeName Core.Project.Data.Migrations.Configuration -ProjectName Core.Project.Data


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow