0

I am learning ASP.NET Core Web API, and I created a project about movies for doing crud operations. I created the project and my mentor told me to create a new class library and write all methods and data operations from that library, and add a reference to the main project and use dependency injection. But while I try to migrate the data, I get an error:

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

I am using Entity Framework Core and SQL database

DbContext class

using Microsoft.EntityFrameworkCore;
using MovieManagementLibrary.Models;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace MovieManagementLibrary.Data
{
    public class MovieDbContext : DbContext
    {
        public MovieDbContext(DbContextOptions<MovieDbContext> options) : base(options)
        {
        }

        public DbSet<Movie> Movies { get; set; }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
el halcon
  • 41
  • 5
  • 1
    See https://learn.microsoft.com/en-us/ef/core/cli/powershell regarding "target project" vs "startup project". And you may have to create one migration and move it. – Jeremy Lakeman Mar 22 '23 at 05:35
  • 1
    You can see this page https://stackoverflow.com/questions/38705694/add-migration-with-different-assembly – firatt_ Mar 22 '23 at 05:43
  • @JeremyLakeman Solve it there is an option for selecting default project and I need to change it – el halcon Mar 22 '23 at 05:50

2 Answers2

0

You should be able to change the Migration Assembly during the service collection configuration.

        serviceCollection.AddPooledDbContextFactory<AppDbContext>(dbContextOptionsBuilder =>
        {
            dbContextOptionsBuilder.UseMySql(connectionString,
                new MySqlServerVersion(environmentConfiguration.DbVersionString),
                builder => builder.MigrationsAssembly("YOUR PROJECT NAME"));
        });

Note: Change the "YOUR PROJECT NAME"

Venkat S
  • 23
  • 5
0
*public class MovieDbContext : DbContext
{
    public MovieDbContext(DbContextOptions<MovieDbContext> options) : base(options)
    {
    }
    public DbSet<Movie> Movies { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("your connection string", options => options.MigrationsAssembly("MovieAPI"));
        }
    }
}*