Entity Framework 6 (EF6) code first migrations with models in a separate project

Using EF6 code migrations, I can successfully save the models and create new migrations for them. However, my DbContext class is in the project Sharp.Data

, the actual (Sql CE) database is in the bin folder of the project Sharp.Server

, and my models live in the project Sharp.Common

.

When I run add-migration -ProjectName Sharp.Data Migration3

(pointing to Sharp.Data

where the DbContext is) it launches successfully and identifies the changes made to the project models Sharp.Common

. However, when I run update-database -ProjectName Sharp.Data

it updates / creates / migrates / creates a new database located in the bin folder of my project Sharp.Data

instead of Sharp.Server

where the application ultimately reads data from.

Is there a way to do this? For the migration to create / update a database that exists somewhere other than its own project file? This may be a relic of using SQL CE, but this is my database for various reasons.

0


source to share


2 answers


You can pass additional parameters update-database

that allow:

  • Please specify a different connection string
  • Create a SQL script that you can apply to the target DB

Full syntax update-database

:

Update-Database [-SourceMigration <String>] [-TargetMigration <String>]
[-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] 
[-ConfigurationTypeName <String>] [-ConnectionStringName <String>] 
[<CommonParameters>]

Update-Database [-SourceMigration <String>] [-TargetMigration <String>] 
[-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] 
[-ConfigurationTypeName <String>] -ConnectionString <String> 
-ConnectionProviderName <String> [<CommonParameters>]

      



To specify a connection string (which points to the correct folder in your project):

  • -StartUpProjectName

    and -ConnectionStringName

    to specify a project containing a .config file with a connection string with the specified name (1st syntax)
  • -ConnectionString

    to specify the connection string directly (second syntax)

To create a SQL script that can be applied directly to the DB using another tool:

  • -SourceMigration

    : the current migration to the target DB -TargetMigration

    : the migration to update and -Script

    : to create a max script that you can apply manually
+2


source


@ JotaBe's answer is correct. But to summarize, you need to do the following:



add-migration "MigrationName" -ProjectName Sharp.Data -ConnectionString "ConnectionStringToYourTargetDatabaseHere"

      

0


source







All Articles