1

когда пишу dotnet run выдет ошибку:

Cannot consume scoped service 'wandio.Models.AppDbContext' from singleton 'wandio.Models.IMobileRepository'

HomeController

namespace wandio.Controllers
{
    public class HomeController : Controller
    {
        private readonly IMobileRepository _mobileRepository;
        public HomeController(IMobileRepository mobileRepository)
        {
            _mobileRepository = mobileRepository;
        }
        public IActionResult index()
        {
            return View();
        }
    }
}



using System.Collections.Generic;

namespace wandio.Models
{
    public interface IMobileRepository
    {
        IEnumerable<Mobile> GetAllMobiles();
        Mobile GetMobileId(int mobileId);
    }
}



namespace wandio.Models
{
    public class MobileRepository : IMobileRepository
    {
        private readonly AppDbContext _appDbContext;
        public MobileRepository(AppDbContext appDbContext)
        {
            _appDbContext = appDbContext;
        }
        public IEnumerable<Mobile> GetAllMobiles()
        {
            return _appDbContext.Mobile;
        }

        public Mobile GetMobileId(int mobileId)
        {
            return _appDbContext.Mobile.FirstOrDefault(m => m.Id == mobileId);
        }
    }
}
1
  • В классе Startup замените services.AddSingleton<IMobileRepository, MobileRepository>(); на services.AddScoped<IMobileRepository, MobileRepository>();
    – Ruslan_K
    19 сен 2019 в 2:36

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.