Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add migration with different assembly

I am working on a project with ASP.NET CORE 1.0.0 and I am using EntityFrameworkCore. I have separate assemblies and my project structure looks like this:

ProjectSolution    -src       -1 Domain          -Project.Data       -2 Api          -Project.Api 

In my Project.Api is the Startup class

public void ConfigureServices(IServiceCollection services)     {                     services.AddDbContext<ProjectDbContext>();          services.AddIdentity<IdentityUser, IdentityRole>()                 .AddEntityFrameworkStores<ProjectDbContext>()                 .AddDefaultTokenProviders();     } 

The DbContext is in my Project.Data project

public class ProjectDbContext : IdentityDbContext<IdentityUser> {     public ProjectDbContext(DbContextOptions<ProjectDbContext> options) : base(options)     {      }      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)     {          var builder = new ConfigurationBuilder();         builder.SetBasePath(Directory.GetCurrentDirectory());         builder.AddJsonFile("appsettings.json");         IConfiguration Configuration = builder.Build();          optionsBuilder.UseSqlServer(             Configuration.GetConnectionString("DefaultConnection"));         base.OnConfiguring(optionsBuilder);     } } 

When I try to make the initial migration, I get this error:

"Your target project 'Project.Api' doesn't match your migrations assembly 'Project.Data'. 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("Project.Api")). 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."

After I seeing this error, I tried to execute this command located in Project.Api:

dotnet ef --startup-project ../Project.Api --assembly "../../1 Data/Project.Data" migrations add Initial

and I got this error:

"Unexpected value '../../1 Domain/Project.Data' for option 'assembly'"

I don't know why I get this error, when I try to execute the command with the '-assembly' parameter.

I can't create a Initial Migration from other assembly and I've searched for information about it but didn't got any results.

Has someone had similar issues?

like image 382
kdar Avatar asked Aug 01 '16 18:08

kdar


People also ask

How do you add migration to multiple contexts?

Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.

Can we have multiple DbContext in Entity Framework?

In code first, you can have multiple DBContext and just one database. You just have to specify the connection string in the constructor. Yes you can, but how can you query from different entities from different db contexts?

How do I run an add migration in Visual Studio?

Adding a Migration So, firstly, you need to create a migration. Open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and execute the following command to add a migration.


2 Answers

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, Project.Data.dll. As the error message suggests, you have have a two options to resolve this

1 - Change target assembly.

cd Project.Data/ dotnet ef --startup-project ../Project.Api/ migrations add Initial  // code doesn't use .MigrationsAssembly...just rely on the default options.UseSqlServer(connection) 

2 - Change the migrations assembly.

cd Project.Api/ dotnet ef migrations add Initial  // change the default migrations assembly options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api")) 
like image 173
natemcmaster Avatar answered Oct 09 '22 11:10

natemcmaster


I had the same problem until I noticed that on the package manager console top bar => "Default Projects" was supposed to be "Project.Data" and not "Project.API".

Once you target the "Project.Data" from the dropdown list and run the migration you should be fine.

default project selection

like image 24
Dre Ross Avatar answered Oct 09 '22 13:10

Dre Ross