The Issue

dotnet : Your target project 'PVStore.Web' doesn't match your migrations assembly 'PVStore.Data'. Either change your target project or change your migrations assembly.
At line:1 char:1
+ dotnet ef migrations add initial
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Your target pro...tions assembly.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("PVStore.Web")). 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.

Solution

Depend on your solution and projects setup in Visual Studio, sometime Entity Framework Code First commands don’t run as expected.

By default, when Code First migration commands run, it will pick the default project set on your Package Manager Console.

But most of the time, we have separate projects in Visual Studio solutions for presentation, business and data layers.

If you still want to run commands from the Package Manager Console as we did the only thing to do is navigate to the project’s root first using a cd path command and then run the command.

Step 1: Change your migrations assembly in Starup.cs file

services.AddDbContext<PVStoreContext>(options =>
options.UseSqlServer(Configuration["Data:PVStoreConnection:ConnectionString"], b => b.MigrationsAssembly("PVStore.Web")));

Step 2: Change the directory to the default path

PM> cd ..\PVStore.Web

step 3: Use “migrations [command] –help” for more information about a command.

PM> dotnet ef migrations add --help

Usage: dotnet ef migrations add [arguments] [options]
Arguments:
[name] The name of the migration
Options:
-o|--output-dir <path> The directory (and sub-namespace) to use. If omitted, "Migrations" is used. Relative paths are relative the directory in which the command is executed.
-c|--context <context> The DbContext to use. If omitted, the default DbContext is used
-e|--environment <environment> The environment to use. If omitted, "Development" is used.
--json Use json output
-h|--help Show help information
-v|--verbose Enable verbose output

Step 4: Execute commands again

PM> dotnet ef migrations add initial

And Done

dotnet : Done. To undo this action, use 'ef migrations remove'
At line:1 char:1
+ dotnet ef migrations add initial
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Done. To undo t...rations remove':String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PM> dotnet ef database update
Project PVStore.Models (.NETStandard,Version=v1.5) was previously compiled. Skipping compilation.
Project PVStore.Data (.NETStandard,Version=v1.5) was previously compiled. Skipping compilation.
Project PVStore.Services (.NETStandard,Version=v1.5) was previously compiled. Skipping compilation.
Project PVStore.Web (.NETCoreApp,Version=v1.0) will be compiled because Input items added from last build
Compiling PVStore.Web for .NETCoreApp,Version=v1.0
Compilation succeeded.
 0 Warning(s)
 0 Error(s)
Time elapsed 00:00:02.4247800
Advertisements