我尝试添加身份到我的MVC项目。我已经添加了标识脚手架项目,但是我添加后项目不会启动。
这是我得到的错误代码:
系统。AggregateException:某些服务无法被构造(在验证服务描述符"ServiceType"时出现错误): KjeksFabrikken.DataAccess.Repository. irepository . iunitofwork生命周期:Scoped实现类型:KjeksFabrikken.DataAccess.Repository。联合国UnitOfWork":能够解析类型为"KjeksFabrikken.DataAccess.Data"的服务。在尝试激活'KjeksFabrikken.DataAccess.Repositor '时,使用ApplicationDbContext'y.UnitOfWork"。)——比;系统。InvalidOperationException:在验证服务描述符时出错。ServiceType: KjeksFabrikken.DataAccess.Repository.IRepository.IUnitOfWork生命周期:范围实现类型:KjeksFabrikken.DataAccess.Repository。UnitOfWork':无法解析类型'KjeksFabrikken '的服务。DataAccess.Data。在尝试激活'KjeksFabrikken.DataAccess.Repository.UnitOfWork'时,使用ApplicationDbContext'。——比;系统。InvalidOperationException:无法解析类型'KjeksFabrikken.DataAccess.Data '的服务。ApplicationDbContext'在尝试操作时ivate"KjeksFabrikken.DataAccess.Repository.UnitOfWork"。在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory。CreateArgumentCallSites(类型实现类型,CallSiteChain callSiteChain、ParameterInfo[]参数,布尔值在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory。CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type impl .ementationType, CallSiteChain在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory。TryCreateExact(ServiceDescriptor)描述符,Type serviceType, CallSiteChain callSiteChain, Int32 slot)在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory。GetCallSite(ServiceDescriptor ServiceDescriptor, CallSiteChainn)在Microsoft.Extensions.DependencyInjection.ServiceProvider。ValidateService (ServiceDescriptor描述符)——内部异常堆栈跟踪结束——在Microsoft.Extensions.DependencyInjection.ServiceProvider。ValidateService (ServiceDescriptor描述符)在Microsoft.Extensions.DependencyInjection.ServiceProvider . .ctor(ICollection
1 serviceDescriptors, ServiceProviderOptions options) --- End of inner exception stack trace --- at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection
1 serviceDescriptors, ServiceProviderOptions options)在Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions。BuildServiceProvider(IServiceCollection servicesiderOptions选项)在Microsoft.Extensions.Hosting.HostApplicationBuilder.Build ()在Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build ()在程序。$(String[] args) in C:UsersDavidRiderProjects3KjeksFabrikkenKjeksFabrikkenProgram.cs:line 18
这是我的ApplicationDbContext.cs文件:
using KjeksFabrikken.Models.Models;
using KjeksFabrikken.ViewModels;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Cookie = KjeksFabrikken.Models.Models.Cookie;
namespace KjeksFabrikken.DataAccess.Data;
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Cookie> Cookies { get; set; }
public DbSet<Ingredient> Ingredients { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<CookieCategory> CookieCategories { get; set; }
public DbSet<CookieIngredient> CookieIngredients { get; set; }
public DbSet<ContactInput> ContactInputs { get; set; }
}
这是我的IUnitOfWork.cs文件:
namespace KjeksFabrikken.DataAccess.Repository.IRepository;
public interface IUnitOfWork
{
ICookieRepository Cookie { get; }
ISaveContactInputRepository SaveContactInput { get; set; }
ISendContactInputRepository SendContactInput { get; set; }
void Save();
}
这是我的UnitOfWork.cs文件:
using KjeksFabrikken.DataAccess.Data;
using KjeksFabrikken.DataAccess.Repository.IRepository;
namespace KjeksFabrikken.DataAccess.Repository;
public class UnitOfWork : IUnitOfWork
{
private ApplicationDbContext _db;
public UnitOfWork(ApplicationDbContext db)
{
_db = db;
Cookie = new CookieRepository(_db);
SaveContactInput = new SaveContactInputRepository(_db);
SendContactInput = new SendContactInputRepository(_db);
}
public ICookieRepository Cookie { get; set; }
public ISaveContactInputRepository SaveContactInput { get; set; }
public ISendContactInputRepository SendContactInput { get; set; }
public void Save()
{
try
{
_db.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
最后,这是我的Program.cs文件:
using KjeksFabrikken.DataAccess.Data;
using KjeksFabrikken.DataAccess.Repository;
using KjeksFabrikken.DataAccess.Repository.IRepository;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")
));
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
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.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
下面是我在项目中安装的NuGet包:
安装在MVC项目上的Nuget Packages
我是新的MVC和Asp。所以我不知道问题是什么。我不理解错误消息,我知道applicationdbcontext和unitofwork在添加标识之前工作得很好。请帮助。
我终于找到了这个问题的根源和解决办法。添加标识(支架项)后,一个"区域"文件夹已在项目中生成。在《身份》中文件夹内的"区域";文件夹中,还会生成一个"Data"文件夹中。("Areas"→"Identity"→"Data"。
在"Data"文件夹中,身份支架项生成另一个ApplicationDbContext.cs"文件。因此,应用程序生成了另一个applicationdbcontext文件,即使我之前已经有了一个。
删除"Data"文件夹(在"身份"中)文件夹),并生成"ApplicationDbContext.cs"文件,应用程序终于工作了。