实体框架核心无法添加迁移

时间:2018-01-25 10:06:52

标签: asp.net-core visual-studio-code .net-core entity-framework-core entity-framework-core-migrations

执行dotnet ef add migration InitialMigration命令,发出此错误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.

我在启动类上的ConfigureServices方法如下:

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

VegaContext课程是:

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

csproj文件的一部分是:

<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 个答案:

答案 0 :(得分:0)

如果您的代码正是您发布的内容,那么解决方案非常简单。只需将你的DbContext构造函数公开,一切都应该没问题:

CASE