簡體   English   中英

實體框架核心無法添加遷移

[英]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. 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.

我在Startup類上的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>

如果您的代碼正是您發布的代碼,則解決方案非常簡單。 只需將您的DbContext-constructor公開,一切就可以了:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM