当我尝试添加迁移时,此错误不断弹出:
无法为设计时支持的不同模式创建类型为“applicationdbcontext”的对象
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.AddControllersWithViews();
}
// 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.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
字符串
ApplicationDBContext.cs代码:
public class ApplicationDBContext : DbContext
{
public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options)
: base(options)
{
}
public DbSet<Item> Items { get; set; }
}
型
2条答案
按热度按时间4ioopgfo1#
ASP.NET Core应用程序使用依赖项注入进行配置。EF Core可以使用Startup.cs的ConfigureServices方法中的AddDbContext添加到此配置。例如:
字符串
您也可以使用简单DbContext初始化与'新'初始化它
型
cl25kdpy2#
在AddDbContext之后添加
var app = builder.Build();
。的数据