Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

.net core - How can I solve the error in my API project?

I added an API project to my project. Although I have given all references when I run it, I get the following error. How can I fix? enter image description here


This is my startup file:

public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }

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

Although I have added all references in my project, I am encountering this error. Despite a lot of research, I could not figure it out. Please help me. If I have other project files I need to add, I can.


This is my EfCoreProductRepository file:

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


       
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to register your context type for dependency injection:

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

Note, this example assumes you are using SQLServer. There are equivalent methods to register other database providers.

You can find the Microsoft docs page here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...