1
good afternoon.
When trying to generate an initial Migration to create the database structure, I get the following error: "Unable to create an Object of type 'Meudbcontext'. For the Different Patterns supported at design time, see https://go.microsoft.com/fwlink/? linkid=851728."
Using Verbose when executing the command, this is my return:
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\Users\marcos.reis\Documents\Desenvolvedor.IO\MinhaAPICompleta\src\DevIO.Data\bin\Debug\netcoreapp3.0\DevIO.Data.deps.json --additionalprobingpath C:\Users\marcos.reis\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Users\marcos.reis\Documents\Desenvolvedor.IO\MinhaAPICompleta\src\DevIO.Data\bin\Debug\netcoreapp3.0\DevIO.Data.runtimeconfig.json C:\Users\marcos.reis\.nuget\packages\microsoft.entityframeworkcore.tools\3.1.0\tools\netcoreapp2.0\any\ef.dll migrations add Initial --json --verbose --no-color --prefix-output --assembly C:\Users\marcos.reis\Documents\Desenvolvedor.IO\MinhaAPICompleta\src\DevIO.Data\bin\Debug\netcoreapp3.0\DevIO.Data.dll --startup-assembly C:\Users\marcos.reis\Documents\Desenvolvedor.IO\MinhaAPICompleta\src\DevIO.Data\bin\Debug\netcoreapp3.0\DevIO.Data.dll --project-dir C:\Users\marcos.reis\Documents\Desenvolvedor.IO\MinhaAPICompleta\src\DevIO.Data\ --language C# --working-dir C:\Users\marcos.reis\Documents\Desenvolvedor.IO\MinhaAPICompleta --root-namespace DevIO.Data
Using assembly 'DevIO.Data'.
Using startup assembly 'DevIO.Data'.
Using application base 'C:\Users\marcos.reis\Documents\Desenvolvedor.IO\MinhaAPICompleta\src\DevIO.Data\bin\Debug\netcoreapp3.0'.
Using working directory 'C:\Users\marcos.reis\Documents\Desenvolvedor.IO\MinhaAPICompleta\src\DevIO.Data'.
Using root namespace 'DevIO.Data'.
Using project directory 'C:\Users\marcos.reis\Documents\Desenvolvedor.IO\MinhaAPICompleta\src\DevIO.Data\'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'MeuDbContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'MeuDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.MissingMethodException: No parameterless constructor defined for type 'DevIO.Data.Context.MeuDbContext'.
at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_3.<FindContextTypes>b__13()
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_3.<FindContextTypes>b__13()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create an object of type 'MeuDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Can you help me? Below are my Context and Startup, and I am mapping the entities correctly.
public void ConfigureServices(IServiceCollection services)
{
services.ResolveDependencies();
services.AddDbContext<MeuDbContext>(options =>
{
options.UseSqlServer("DefaultConnection");
});
services.AddAutoMapper(typeof(Startup));
services.AddControllers();
}
public static IServiceCollection ResolveDependencies(this IServiceCollection services)
{
services.AddScoped<MeuDbContext>();
services.AddScoped<IProdutoRepository, ProdutoRepository>();
services.AddScoped<IFornecedorRepository, FornecedorRepository>();
services.AddScoped<IEnderecoRepository, EnderecoRepository>();
//services.AddSingleton<IValidationAttributeAdapterProvider, MoedaValidationAttributeAdapterProvider>();
services.AddScoped<INotificador, Notificador>();
services.AddScoped<IFornecedorService, FornecedorService>();
services.AddScoped<IProdutoRepository, ProdutoRepository>();
services.AddScoped<IProdutoService, ProdutoService>();
return services;
}
CONTEXTO:
public MeuDbContext(DbContextOptions options) : base(options)
{
}
CONNECTION STRING
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MinhaApiCore;Trusted_Connection=true;MultipleActiveResultSets=true"
}
If you have any errors in your project, Migrations will not run. Including dependency injection errors. In the case of your project, you have the error " Static method 'Createhostbuilder(string[])' was found on class 'Program'. That’s why Migrations is not running.
– Raquel Pinheiro