[英]No parameterless constructor defined for this object. MVC 5
I have a problem when i try to code with .NET Framework(4.7.2) and EF Core(3.0) because usually i use .NET Core.当我尝试使用 .NET Framework(4.7.2) 和 EF Core(3.0) 进行编码时遇到问题,因为我通常使用 .NET Core。 When i try to inject Repository(by its interface).
当我尝试注入 Repository(通过其接口)时。
This is my code这是我的代码
SenderController.cs发件人控制器.cs
public class SenderController : Controller
{
private readonly ISenderRepository _sRepo;
public SenderController(ISenderRepository sRepo)
{
_sRepo = sRepo;
}
public ActionResult Index()
{
return View();
}
ISenderRepository.cs ISenderRepository.cs
public interface ISenderRepository
{
ICollection<Sender> GetSenders();
Sender GetSender(int senderId);
bool SenderExists(string name);
bool SenderExists(int id);
bool CreateSender(Sender sender);
bool UpdateSender(Sender sender);
bool DeleteSender(Sender sender);
bool Save();
}
and this is what i get这就是我得到的
i think i do wrong way for dependency injection in MVC 5. Can you help me to do that?我想我在 MVC 5 中做依赖注入的方式是错误的。你能帮我做到这一点吗? Thank you :)
谢谢 :)
I just fixing it by using this statement in constructor :我只是通过在构造函数中使用此语句来修复它:
private ISenderRepository _sRepo;
public SenderController()
{
this._sRepo = new SenderRepository(new Data.ApplicationDbContext());
}
Thanks谢谢
确保你必须注入你的控制器,你可以使用 Autofac 来帮助你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.