Question

Executing dotnet ef add migration InitialMigration command giving this error A suitable constructor for type 'Vega.Repository.VegaContext' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

My ConfigureServices method on Startup class is following:

public void ConfigureServices(IServiceCollection services)
{            
   services.AddMvc();       
   services.AddScoped<IUnitOfWork, UnitOfWork>();
   var connectionString = Configuration.GetConnectionString("VegaConnection");
   services.AddDbContext<VegaContext>(options =>
            options.UseSqlServer(connectionString));
}

VegaContext class is:

public class VegaContext : DbContext
{
    VegaContext(DbContextOptions<VegaContext> options):base(options)
    {}
    public DbSet<Make> Makes { get; set; }
}

Part of csproj file is:

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
</ItemGroup>

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
1
1
1/25/2018 10:06:51 AM

Accepted Answer

If your code is exactly what you´ve posted, the solution is pretty simple. Just make your DbContext-constructor public and everything should be fine:

public class VegaContext : DbContext
{
    public VegaContext(DbContextOptions<VegaContext> options):base(options) { }
    public DbSet<Make> Makes { get; set; }
}
1
1/26/2018 11:57:53 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow