我正在跟随一个Instructor,一开始一切都很顺利,但是当我将项目发布到github后,项目的文件发生了变化,我认为我做错了什么,后来我编辑了内容。但有一点我错过了,我认为我无法建立数据库连接(或者我误解了它)我也尝试了有关此主题的页面上的解决方案,但它不起作用。请你帮助我好吗?
我得到的错误如下
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Repositories.RepositoryContext Lifetime: Scoped ImplementationType: Repositories.RepositoryContext': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet
1[Entities.Models.Product]' 尝试激活 'Repositories.RepositoryContext' 时。)(验证服务描述符 'ServiceType: Repositories.Contracts.IRepositoryManager Lifetime: Scoped ImplementType: Repositories.RepositoryManager' 时出错:无法解析服务1[Entities.Models.Product]' while attempting to activate 'Repositories.RepositoryContext'.) (Error while validating the service descriptor 'ServiceType: Repositories.Contracts.IProductRepository Lifetime: Scoped ImplementationType: Repositories.ProductRepository': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet
尝试激活“Repositories.RepositoryContext”时键入“Microsoft.EntityFrameworkCore.DbSet 1[Entities.Models.Product]”。)
InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet
1[Entities.Models.Product]',同时尝试激活“Repositories.RepositoryContext”。`
我一一检查了我的存储库层,一一检查了我的 Program.cs 链接,我检查了微软网站上的说明,但我想我无法以我目前的知识水平解决这个问题。
我的存储库上下文页面如下所示
public class RepositoryContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
public RepositoryContext(DbContextOptions<RepositoryContext> options, DbSet<Product> products, DbSet<Category> categories) :
base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Product>()
.HasData(new Product() { Id = 1, ProductName = "Laptop", Price = 1000 },
new Product() { Id = 2, ProductName = "Keyboard", Price = 500 },
new Product() { Id = 3, ProductName = "Computer", Price = 2500 },
new Product() { Id = 4, ProductName = "Monitor", Price = 1000 },
new Product() { Id = 5, ProductName = "Deck", Price = 500 }
);
modelBuilder.Entity<Category>()
.HasData(
new Category() { CategoryId = 1, CategoryName = "Book" },
new Category() { CategoryId = 2, CategoryName = "Electronic" });
}
}
这是我在程序开头的代码
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddControllersWithViews();
builder.Services.AddDbContext<RepositoryContext>(options =>
{
options.UseSqlite(builder.Configuration.GetConnectionString
("sqlconnection"),
b => b.MigrationsAssembly("StoreApp"));
});
builder.Services.AddScoped<IRepositoryManager, RepositoryManager>();
builder.Services.AddScoped<IProductRepository, ProductRepository>();
var app = builder.Build();
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
物体有这个形状
public class Product
{
public int Id { get; set; }
public String? ProductName { get; set; } = String.Empty;
public decimal Price { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public String? CategoryName { get; set; }= String.Empty;
}
你不应该注入
DbSet
's,只需将它们从 ctor 中删除:或者:
处理可空性警告(如果有)。
也可以看看:
您需要将什么注入
DbSet<Product>
构造DbSet<Category>
函数中?如果构造函数中没有这些参数,应该可以正常工作。这两个参数无法构造,这就是导致服务解析异常的原因。