Migration does not recognize Context

Asked

Viewed 222 times

1

I’m developing an application using DDD architecture. In my Infra layer, I have my data models, all based on Code First. I’m using the SQL as database. I have installed everything that was needed.

It turns out that when I try to apply the Migration get the error:

PM> Add-Migration -MuoContext Initial
Add-Migration : Não é possível localizar um parâmetro que coincida com o nome de parâmetro 'MuoContext'.
No linha:1 caractere:15
+ Add-Migration -MuoContext Initial
+               ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-Migration], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Add-Migration

The parameter Muocontext is the name of my class, as can be seen below:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Muo.Domain.Areas.BI.Entities;
using Muo.Domain.Areas.Commons.Entities;
using Muo.Infrastructure.Data.Mappings.Areas.BI;
using Muo.Infrastructure.Data.Mappings.Areas.Commons;
using System;
using System.IO;
using System.Linq;

namespace Muo.Infrastructure.Data.Context
{
    public class MuoContext : DbContext
    {
        #region DbSets
        //DbSets - ../Domain/Areas/Commons/Entities
        public DbSet<CnaeActivity> CnaeActivities { get; set; }
        public DbSet<Company> Companies { get; set; }
        public DbSet<CompanyAddress> CompanyAddresses { get; set; }

        //DbSets - ../Domain/Areas/BI/Entities
        public DbSet<ChartOfAccountStandardDfp> ChartOfAccountStandardDfps { get; set; }
        #endregion
        #region Configurations

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //Mappings/Areas/BI
            modelBuilder.ApplyConfiguration(new ChartOfAccountStandardDfpMap());

            //Mappings/Areas/Commons
            modelBuilder.ApplyConfiguration(new CompanyMap());
            modelBuilder.ApplyConfiguration(new CompanyAddressMap());
            modelBuilder.ApplyConfiguration(new CnaeActivityMap());

            base.OnModelCreating(modelBuilder);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            optionsBuilder.UseSqlServer(config.GetConnectionString("DefaultConnection"));
        }

My acquittal appsettings.json is like this:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(LocalDb)\\MSSQLLocalDB;Database=MuoDB;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

I don’t understand and I can’t find a solution.

  • Run Enable-Migrations

  • Thank you @Netinhosantos, but Enable-Migrations is obsolete in Efcore. The correct is Add-Migration even.

1 answer

0

I think you’re confusing it with the parameter.

The right thing would be:

Add-Migration --context MuoContext Initial 

or

Add-Migration -c MuoContext Initial

But if your application only has 1 context and the project you will generate Migration is the same as in the context then you don’t even need to inform:

Add-Migration Initial

Browser other questions tagged

You are not signed in. Login or sign up in order to post.