el halcon
el halcon

Reputation: 59

Change migration assembly by using dbcontext in ASP.NET Core Web API

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; }
    }
}

Upvotes: 1

Views: 1869

Answers (2)

Amine Nafkha
Amine Nafkha

Reputation: 19

*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"));
        }
    }
}*

Upvotes: 1

Venkat S
Venkat S

Reputation: 43

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"

Upvotes: 1

Related Questions