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

Migration in different assembly than target assembly not supported #5900

Closed
VikSab opened this Issue Jun 29, 2016 · 12 comments

Comments

Projects
None yet
7 participants
@VikSab

VikSab commented Jun 29, 2016

Steps to reproduce

Create Web project that injects and initializes the DbContext and a Models project with the DbContext and migrations

The issue

Your target project '#Target Project#' doesn't match your migrations assembly '#Migrations Assembly#'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("#Target Project#")). By default, the migrations assembly is the assembly containing the DbContext.
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.

Exception message:
Stack trace:
Your target project '<Target Project>' doesn't match your migrations assembly '<Migrations Assembly>'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("<Target Project>'")). By default, the migrations assembly is the assembly containing the DbContext.
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.

### Further technical details

EF Core version: 1.0.0 RTM
Operating system:  Windows 10

Visual Studio version: 2015 -Command line Dotnet 1.0.0

Other details about my project setup:
@rowanmiller

This comment has been minimized.

Show comment
Hide comment
@rowanmiller

rowanmiller Jun 29, 2016

Member

@natemcmaster can you give guidance on this

Member

rowanmiller commented Jun 29, 2016

@natemcmaster can you give guidance on this

@natemcmaster

This comment has been minimized.

Show comment
Hide comment
@natemcmaster

natemcmaster Jun 29, 2016

Member

@VikSab This is a common error when working with a DbContext that is in a separate project from your web app project. Can you share more details about which projects you are using and the file structure of your code?

Member

natemcmaster commented Jun 29, 2016

@VikSab This is a common error when working with a DbContext that is in a separate project from your web app project. Can you share more details about which projects you are using and the file structure of your code?

@VikSab

This comment has been minimized.

Show comment
Hide comment
@VikSab

VikSab Jun 29, 2016

TestWeb.zip
I have uploaded the Test Project that i was able to reproduce the issue

VikSab commented Jun 29, 2016

TestWeb.zip
I have uploaded the Test Project that i was able to reproduce the issue

@natemcmaster

This comment has been minimized.

Show comment
Hide comment
@natemcmaster

natemcmaster Jun 29, 2016

Member

Thanks for the repro. You will see the error when invoking EF commands on the "Web" project.

The exception message you are seeing is by-design.


All EF commands have this check:

if (targetAssembly != migrationsAssembly) 
       throw MigrationsAssemblyMismatchError;
  • targetAssembly = the target project you are operating on. On the command line, it is the project in the current working directory. In Package Manager Console, it is whatever project is selected in the drop down box on the top right of that window pane.
  • migrationsAssembly = assembly containing code for migrations. This is configurable. By default, this will be the assembly containing the DbContext, in your case, Model.dll.

As the error message suggests, you have have a two options to resolve this

1 - Change target assembly.

cd ../Model
dotnet ef --startup-project ../TestWeb/ migrations add Initial

2 - Change the migrations assembly.

options.UseSqlServer(connection, b => b.MigrationsAssembly("Web"))

If you are still getting this error message even after changing either your migration assembly or your target assembly, please feel free to re-open the issue.

Member

natemcmaster commented Jun 29, 2016

Thanks for the repro. You will see the error when invoking EF commands on the "Web" project.

The exception message you are seeing is by-design.


All EF commands have this check:

if (targetAssembly != migrationsAssembly) 
       throw MigrationsAssemblyMismatchError;
  • targetAssembly = the target project you are operating on. On the command line, it is the project in the current working directory. In Package Manager Console, it is whatever project is selected in the drop down box on the top right of that window pane.
  • migrationsAssembly = assembly containing code for migrations. This is configurable. By default, this will be the assembly containing the DbContext, in your case, Model.dll.

As the error message suggests, you have have a two options to resolve this

1 - Change target assembly.

cd ../Model
dotnet ef --startup-project ../TestWeb/ migrations add Initial

2 - Change the migrations assembly.

options.UseSqlServer(connection, b => b.MigrationsAssembly("Web"))

If you are still getting this error message even after changing either your migration assembly or your target assembly, please feel free to re-open the issue.

@VikSab

This comment has been minimized.

Show comment
Hide comment
@VikSab

VikSab Jun 29, 2016

Looks good i was placing the --startup-project on the database command and not the ef command..

the help file doesnt seem to have this option

dotnet ef --help
Project Model (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Project TestWeb (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Entity Framework .NET Core CLI Commands 1.0.0-preview2-21431
Usage: dotnet ef [options] [command]
Options:
-h|--help Show help information
-v|--verbose Enable verbose output
--version Show version information
--assembly The assembly file to load.
--startup-assembly The assembly file containing the startup class.
--data-dir

The folder used as the data directory (defaults to current working directory).
--project-dir The folder used as the project directory (defaults to current working directory).
--content-root-path The folder used as the content root path for the application (defaults to application base directory).
--root-namespace The root namespace of the target project (defaults to the project assembly name).
Commands:
database Commands to manage your database
dbcontext Commands to manage your DbContext types
migrations Commands to manage your migrations
Use "dotnet ef [command] --help" for more information about a command.

VikSab commented Jun 29, 2016

Looks good i was placing the --startup-project on the database command and not the ef command..

the help file doesnt seem to have this option

dotnet ef --help
Project Model (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Project TestWeb (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Entity Framework .NET Core CLI Commands 1.0.0-preview2-21431
Usage: dotnet ef [options] [command]
Options:
-h|--help Show help information
-v|--verbose Enable verbose output
--version Show version information
--assembly The assembly file to load.
--startup-assembly The assembly file containing the startup class.
--data-dir

The folder used as the data directory (defaults to current working directory).
--project-dir The folder used as the project directory (defaults to current working directory).
--content-root-path The folder used as the content root path for the application (defaults to application base directory).
--root-namespace The root namespace of the target project (defaults to the project assembly name).
Commands:
database Commands to manage your database
dbcontext Commands to manage your DbContext types
migrations Commands to manage your migrations
Use "dotnet ef [command] --help" for more information about a command.

@natemcmaster

This comment has been minimized.

Show comment
Hide comment
@natemcmaster

natemcmaster Jun 29, 2016

Member

@VikSab it's there, but the help output doesn't show it. I'm working to fix this in #5188.

See https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html for the "true" set of available parameters.

Member

natemcmaster commented Jun 29, 2016

@VikSab it's there, but the help output doesn't show it. I'm working to fix this in #5188.

See https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html for the "true" set of available parameters.

@VikSab

This comment has been minimized.

Show comment
Hide comment
@VikSab

VikSab Jun 29, 2016

Should have said - that it works.. -

i was trying to apply it - but on the database or update command - didnt try it on the ef command.

The help file update would have made the fix easier to self managed thanks

VikSab commented Jun 29, 2016

Should have said - that it works.. -

i was trying to apply it - but on the database or update command - didnt try it on the ef command.

The help file update would have made the fix easier to self managed thanks

@Defee

This comment has been minimized.

Show comment
Hide comment
@Defee

Defee Nov 13, 2016

@natemcmaster could you advise on following:

  1. My Server model (DB) is separated on various contexts about 5-7 on separated dll's (packages).
  2. The server models will be stored in nuget. So the contexts and configurations of EF API will be stored there too.
  3. I want to run the migrations on my web project that references the packages.
  4. The contexts could target one DB or multiples from various projects.

Earlier those issues can be resolved by storing particular dll and running migrate.exe on them. so the defined migrations in these dlls ran.

How could i use dotnet ef on the particular package to do the migrations defined in the particular package?

Defee commented Nov 13, 2016

@natemcmaster could you advise on following:

  1. My Server model (DB) is separated on various contexts about 5-7 on separated dll's (packages).
  2. The server models will be stored in nuget. So the contexts and configurations of EF API will be stored there too.
  3. I want to run the migrations on my web project that references the packages.
  4. The contexts could target one DB or multiples from various projects.

Earlier those issues can be resolved by storing particular dll and running migrate.exe on them. so the defined migrations in these dlls ran.

How could i use dotnet ef on the particular package to do the migrations defined in the particular package?

@natemcmaster

This comment has been minimized.

Show comment
Hide comment
@natemcmaster

natemcmaster Nov 14, 2016

Member

@Defee please open a new issue for your question.

Member

natemcmaster commented Nov 14, 2016

@Defee please open a new issue for your question.

@jusefb

This comment has been minimized.

Show comment
Hide comment
@jusefb

jusefb Jan 20, 2017

Please Ignore my previous comment I was putting the --startup-project in the wrong place

jusefb commented Jan 20, 2017

Please Ignore my previous comment I was putting the --startup-project in the wrong place

@charce36

This comment has been minimized.

Show comment
Hide comment
@charce36

charce36 May 9, 2018

Thanks a lot, works for me.
dotnet ef --startup-project ../XXXX.API migrations add MigrationName -c MyContext

charce36 commented May 9, 2018

Thanks a lot, works for me.
dotnet ef --startup-project ../XXXX.API migrations add MigrationName -c MyContext

@mshauny

This comment has been minimized.

Show comment
Hide comment
@mshauny

mshauny Sep 11, 2018

dotnet ef --startup-project ../XXXX.API migrations add MigrationName -c MyContext
Works for me too 😄 Thanks

mshauny commented Sep 11, 2018

dotnet ef --startup-project ../XXXX.API migrations add MigrationName -c MyContext
Works for me too 😄 Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment