AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77068302
Accepted
Mehmet
Mehmet
Asked: 2023-09-08 23:50:39 +0800 CST2023-09-08 23:50:39 +0800 CST 2023-09-08 23:50:39 +0800 CST

ASP .NET 无法解析类型的服务

  • 772

我正在跟随一个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.DbSet1[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.DbSet1[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;
        
    }
c#
  • 2 2 个回答
  • 33 Views

2 个回答

  • Voted
  1. Best Answer
    Guru Stron
    2023-09-08T23:52:44+08:002023-09-08T23:52:44+08:00

    你不应该注入DbSet's,只需将它们从 ctor 中删除:

    public class RepositoryContext : DbContext
    {
        public DbSet<Product> Products { get; set; } 
        public DbSet<Category> Categories { get; set; } 
    
    
        public RepositoryContext(DbContextOptions<RepositoryContext> options) :
            base(options) 
        {
           
        }
        ...
    }
    

    或者:

    public class RepositoryContext : DbContext
    {
        public DbSet<Product> Products => Set<Product>();
        public DbSet<Category> Categories => Set<Category>();
    
    
        public RepositoryContext(DbContextOptions<RepositoryContext> options) :
            base(options) 
        {
           
        }
        ...
    }
    

    处理可空性警告(如果有)。

    也可以看看:

    • DbContext 生命周期、配置和初始化
    • EF Core:使用可为 Null 的引用类型
    • 2
  2. Flint0fWalrus
    2023-09-08T23:56:56+08:002023-09-08T23:56:56+08:00

    您需要将什么注入DbSet<Product>构造DbSet<Category>函数中?如果构造函数中没有这些参数,应该可以正常工作。

    这两个参数无法构造,这就是导致服务解析异常的原因。

    • 1

相关问题

  • Polly DecorlatedJitterBackoffV2 - 如何计算完成所有重试所需的最长时间?

  • Wpf。在 ScrollViewer 中滚动 DataGrid

  • 我在使用 .NET MAUI MVVM 的游戏页面上获得的分数在其他页面上不可见。如何在本地设备中保存分数数据

  • 从 DataTemplate 内部将 TreeView 层次结构与 HierarchicalDataTemplate 结合使用

  • 如何改进 .NET 中的验证接口?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve