Entity Framework核心无法添加迁移
Entity Framework Core can not add migration
执行 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
启动方法 class 如下:
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 是:
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>
如果您的代码与您发布的代码完全相同,那么解决方案就非常简单。只需创建您的 DbContext-constructor public,一切都应该没问题:
public class VegaContext : DbContext
{
public VegaContext(DbContextOptions<VegaContext> options):base(options) { }
public DbSet<Make> Makes { get; set; }
}
执行 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
启动方法 class 如下:
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 是:
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>
如果您的代码与您发布的代码完全相同,那么解决方案就非常简单。只需创建您的 DbContext-constructor public,一切都应该没问题:
public class VegaContext : DbContext
{
public VegaContext(DbContextOptions<VegaContext> options):base(options) { }
public DbSet<Make> Makes { get; set; }
}