【问题标题】:How can I solve the error in my API project?如何解决我的 API 项目中的错误?
【发布时间】:2021-04-17 09:00:13
【问题描述】:

我在我的项目中添加了一个 API 项目。虽然我在运行它时给出了所有参考,但我收到以下错误。我该如何解决?


这是我的启动文件:

公共类启动 { 公共启动(IConfiguration 配置) { 配置=配置; }

    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.AddScoped(typeof(ShopContext));
        services.AddDbContext<ShopContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:SqlConStr"].ToString(),
            o =>
            {
                o.MigrationsAssembly("BuyfiletData");
            }));
        services.AddScoped(typeof(IRepository<>), typeof(EfCoreGenericRepository<>));
        services.AddScoped<IProductRepository, EfCoreProductRepository>();
        services.AddScoped<IProductService, ProductManager>();



        services.AddControllers();
    }

    // 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();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

虽然我已经在我的项目中添加了所有引用,但我遇到了这个错误。尽管进行了大量研究,但我无法弄清楚。请帮我。 如果我有其他需要添加的项目文件,我可以。


这是我的 EfCoreProductRepository 文件:

 public class EfCoreProductRepository : EfCoreGenericRepository<Product>, IProductRepository
    {
        
        private ShopContext _context { get=>_context as ShopContext;}

        public EfCoreProductRepository(DbContext context) : base(context)
        {
        }
        public List<Product> GetProductsWithCategory(string name, int page, int pageSize)
        {
            
            
                var products = _context.Products.AsQueryable();
                if (!string.IsNullOrEmpty(name))
                {
                    
                        products = products.Include(i => i.ProductCategories)
                    .ThenInclude(i => i.BasicCategory)
                    .Where(i => i.ProductCategories.Any(a =>
                    a.BasicCategory.Name.ToLower() == name.ToLower() || a.MainCategory.Name.ToLower()==name.ToLower() ||a.SubCategory.Name.ToLower()== name.ToLower()));
                    
                }
                return products.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            
        }

        public List<Product> GetProductsWithFilters(List<Product> productList, int page, int pageSize, int maxPrice, int minPrice)
        {
            
                var products = _context.Products.AsQueryable();
                /*products= products.Where(i => i.Brand == product.Brand && i.Point == product.Point&& i.Variety == product.Variety&& i.Size == product.Size&& i.Color == product.Color&& i.Gender == product.Gender);*/
                foreach (var product in productList)
                {
                    products = products.Where(i =>
                        i.Brand == product.Brand && i.Point == product.Point && i.Variety == product.Variety &&
                        i.Size == product.Size && i.Color == product.Color && i.Gender == product.Gender);
                }

                products.Where(i => i.Price > minPrice && i.Price < maxPrice);
                return products.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            
        }

    

        public List<Product> GetProductsWithSearch(string word,int page, int pageSize)
        {
          
                var products = _context.Products.Where(i=>i.Brand.Contains(word.ToLower())|| i.MainCategory.Contains(word.ToLower())|| i.Category.Contains(word.ToLower()) || i.Details.Contains(word.ToLower())).AsQueryable();
                return products.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            
        }


       
    }

【问题讨论】:

    标签: .net-core entity-framework-core ef-code-first


    【解决方案1】:

    你需要为依赖注入注册你的上下文类型:

    services.AddDbContext<ShopContext>(options =>
                    options.UseSqlServer(connectionString, b =>
                    {
                        // configure options
                    }));
    

    注意,此示例假设您使用的是 SQLServer。也有注册其他数据库提供者的等效方法。

    您可以找到 Microsoft 文档页面 here

    【讨论】:

    • 我添加了。但是,这一次它给出了这个错误:InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContext' while trying to activate 'BuyfiletData.Concrete.EfCore.EfCoreProductRepository'。
    • EfCoreProductRepository构造函数取什么参数?
    • 我更新了问题。我在问题中添加了 Ef Core Product Repository 文件。你可以看看。
    • 这是您需要注册的DbContext。我已经更新了答案。
    • 我更新了启动文件。你可以检查一下。然而,一切都没有改变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 2022-11-28
    • 2021-09-17
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多