Entity framework 添加具有实体框架的控制器失败-DbContext错误
我有一个带有EntityFramework的.Net核心应用程序。 我想添加带有CRUD操作的控制器。 但我犯了这个错误 执行添加迁移时,我没有收到任何错误。它只找到一个DbContext 这些都是我拥有WebApplication8Context的地方 WEBAPPLICATION8CONTEXT.CSEntity framework 添加具有实体框架的控制器失败-DbContext错误,entity-framework,asp.net-core,.net-core,Entity Framework,Asp.net Core,.net Core,我有一个带有EntityFramework的.Net核心应用程序。 我想添加带有CRUD操作的控制器。 但我犯了这个错误 执行添加迁移时,我没有收到任何错误。它只找到一个DbContext 这些都是我拥有WebApplication8Context的地方 WEBAPPLICATION8CONTEXT.CS namespace WebApplication8.Data { public class WebApplication8Context : IdentityDbContex
namespace WebApplication8.Data
{
public class WebApplication8Context : IdentityDbContext<ApplicationUser>
{
public WebApplication8Context(DbContextOptions<WebApplication8Context> options)
: base(options)
{
}
public DbSet<Tasks> Tasks { get; set; }
public DbSet<TaskGroups> TaskGroups { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
[assembly: HostingStartup(typeof(WebApplication8.Areas.Identity.IdentityHostingStartup))]
namespace WebApplication8.Areas.Identity
{
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<WebApplication8Context>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("WebApplication8ContextConnection")));
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<WebApplication8Context>();
});
}
}
}
搜索我的Web应用程序8ModelSnapshot
namespace WebApplication8.Migrations
{
[DbContext(typeof(WebApplication8Context))]
partial class WebApplication8ContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
My Startup.cs
namespace WebApplication8
{
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.AddControllersWithViews();
services.AddRazorPages();
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
}
我可以找到WebApplication8Context的其他地方是我的迁移
我做错了什么?看起来
IdentityHostingStartup
和Startup
之间存在冲突
虽然我不知道具体原因,但您可以使用以下方法解决:
您可以删除IdentityHostingStartup
中的代码,然后将其添加到启动中,如下所示:
services.AddDbContext<WebApplication8Context>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("WebApplication8ContextConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<WebApplication8Context>();
services.AddControllersWithViews();
services.AddRazorPages();
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“WebApplication8ContextConnection”);
services.AddDefaultIdentity(options=>options.SignIn.RequireConfirmedAccount=true)
.AddEntityFrameworkStores();
services.AddControllersWithViews();
services.AddRazorPages();
重新迁移并更新数据库,然后您将成功添加控制器。将代码添加到启动,而不是identityhostingstartup:)谢谢!
services.AddDbContext<WebApplication8Context>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("WebApplication8ContextConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<WebApplication8Context>();
services.AddControllersWithViews();
services.AddRazorPages();