【发布时间】:2019-09-28 10:54:48
【问题描述】:
我正在尝试通过添加实体框架将持久性连接到我的身份服务器。目前,在尝试添加迁移时,我收到错误
找不到名为“ConfigurationDbContext”的 DbContext。
在运行迁移之前,我已将cd'd 放入我的 .csproj 文件所在的目录中,并且正在运行 dotnet ef migrations add InitConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration 以尝试添加迁移。
我的Startup.cs 类如下所示:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
var migrationsAssembly = typeof(ApplicationDbContext).GetTypeInfo().Assembly.GetName().Name;
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddAspNetIdentity<ApplicationUser>()
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
db => db.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
db => db.MigrationsAssembly(migrationsAssembly));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
app.UseMvc();
}
}
我该如何解决这个问题?
编辑:经过进一步调查,当我在生成迁移时使用--project 标志如下:dotnet ef migrations add InitConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration --project BleacherBuddy.IdentityServerService,我收到错误:
MSBUILD:错误 MSB1009:项目文件不存在。无法 检索项目元数据。确保它是基于 MSBuild 的 .NET Core 项目
我目前的猜测是,因为这是一个服务结构项目(无状态 .net 核心),所以构建过程在这里失败并且不允许我生成迁移。经过一些研究,我仍然不确定如何克服这个问题,或者这是否是实际问题。我将简单的身份服务器项目重新创建为 Web api(不使用服务结构),并且能够按预期生成类。感谢所有帮助。
【问题讨论】:
标签: entity-framework .net-core identityserver4 azure-service-fabric