Migration fails after I separated the projects

Viewed 45

Before I separated my application into other projects, everything related to my Models, Database was inside BulkyBookWeb, and after the projects were separated, it looked like this

BulkyBook
/ BulkyBook.DataAccess
/ BulkyBook.Models
/ BulkyBook.Utility
/ BulkyBookWeb

Inside BulkyBook.DataAccess contains my ApplicationDbContext, my migrations and also the repository. And inside BulkyBook.Models contains my representation of the model.

And when running the command BulkyBookWeb git:(main) ✗ dotnet ef migrations add AddCoverTypeToDatabase

Your target project 'BulkyBookWeb' doesn't match your migrations assembly 'BulkyBook.DataAccess'. 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("BulkyBookWeb")). 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.

At BulkyBookWeb at Program.cs I put

builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
builder.Configuration.GetConnectionString("AppDbContext"), option => option.MigrationsAssembly("BulkyBookWeb")
));

But the folder Migration was created inside of the BulkyBookWeb, but I want the migration create inside of BulkyBook.DataAccess inside of Migrations folder using the Model inside of BulkyBook.Models

1 Answers

I found the solution. At Program.cs at BulkyBookWeb I needed to put this line pointing to my other project that contains the migrations.

builder.Services.AddDbContext<ApplicationDbContext>(options => 
    options.UseSqlServer(
        builder.Configuration.GetConnectionString("AppDbContext"), 
        option => option.MigrationsAssembly("BulkyBook.DataAccess")
    )
);
Related