Próbuje dodać do aplikacji kontroler z widokami CRUD.
Dostaję błąd "More than one DbContext named "WebApplication8Context" was found.
Jeśli dobrze rozumiem to mam tylko jeden taki twór. Dlatego nie rozumiem skąd błąd. Ale co kilka par oczu to nie jedne więc może naprowadzicie mnie na to co jest nie tak.
Kiedy robię migracje i update bazy danych program nie wyświetla błędu o większej ilości DbContext.
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using WebApplication8.Areas.Identity.Data;
using WebApplication8.Models;
namespace WebApplication8.Data
{
public class WebApplication8Context : IdentityDbContext<ApplicationUser>
{
public WebApplication8Context(DbContextOptions<WebApplication8Context> options)
: base(options)
{
}
public DbSet<Models.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);
}
}
}
tutaj model, dla którego chce utworzyć widoki
namespace WebApplication8.Models
{
public class Tasks
{
[Required]
public int Id { get; set; }
[Required]
public DateTime CreatedAt { get; set; }
public DateTime? EndedAt { get; set; }
[Required]
[Column(TypeName = "text")]
public string CreatedBy { get; set; }
[Required]
[Column(TypeName = "text")]
public string Title { get; set; }
[Required]
[Column(TypeName = "text")]
public string Description { get; set; }
[Required]
public TaskGroups TaskGroup { get; set; }
[Required]
public string Status { get; set; }
[Column(TypeName = "text")]
public string WantedUser { get; set; }
}
}
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();
services.AddDbContext<WebApplication8Context>(options =>
options.UseSqlServer(Configuration.GetConnectionString("WebApplication8ContextConnection")));
}
// 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();
});
}
}
}
plik json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"WebApplication8ContextConnection": "Server=SERV\\SQLEXPRESS;Database=Application;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}