我是跟着一个导师,一开始一切都很顺利,但是我把项目发布到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:存储库.合约.IRepositoryManager生命周期:作用域实现类型:存储库。存储库管理器“:在尝试激活“Repositories. RepositoryContext”时无法解析类型“Microsoft. Website FrameworkCore.DbSet 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
1[Entities.Models.Product]”的服务。)InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet
1[Entities.Models.Product]“,同时尝试激活”Repositories. RepositoryContext“。`
我一个接一个地检查了我的存储库层,我一个接一个地检查了我的Program.cs链接,我检查了微软网站上的说明,但我想我无法以我目前的知识水平解决这个问题。
我的repositorycontext页面如下所示
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;
}
2条答案
按热度按时间ie3xauqp1#
你不应该注入
DbSet
的,只是从ctor中删除它们:或者:
处理空性警告(如果有)。
另请参阅:
zazmityj2#
为什么需要将
DbSet<Product>
和DbSet<Category>
注入到构造函数中?如果构造函数中没有这些参数,应该可以正常工作。这两个参数无法构造,这就是导致服务解析异常的原因。