文中没有给出任何论点

本文关键字:任何论 文中没 | 更新日期: 2023-09-27 18:03:50

我正在尝试在asp core中实现存储库模式。除了将其添加到控制器之外,一切似乎都进行了一些调整:

    public class HomeController : Controller
    {
        private IDocumentRepository _context;
        public HomeController()
        { 
            _context = new DocumentRepository(new myContext());
        }
}

DocumentRepository.cs

public class DocumentRepository : IDocumentRepository, IDisposable
{
    private myContext context;
    public DocumentRepository(myContext context) : base()
    {
        this.context = context;
    }
    public IEnumerable<Document> GetDocuments()
    {
        return context.Document.ToList();
    }
    public Document GetDocumentByID(int id)
    {
        return context.Document.FirstOrDefault(x => x.Id == id);
    }

IDocumentRepository.cs

public interface IDocumentRepository : IDisposable
{
    IEnumerable<Document> GetDocuments();
    Document GetDocumentByID(int documentId);
    void InsertDocument(Document student);
    void DeleteDocument(int documentID);
    void UpdateDocument(Document document);
    void Save();
}

错误

没有给出符合所需形式的论证参数"options"myContext.myContext (DbContextOptions)dotnetcore . . NETCoreApp, Version = v1.0

文中没有给出任何论点

使用构造函数注入从DI容器中解析IDocumentRepository,而不是手动实例化它,它应该工作:

public class HomeController : Controller  {
    private IDocumentRepository _repository;
    public HomeController(IDocumentRepository repository) { 
        _repository = repository;
    }
}

为此,您需要确保IDocumentRepositoryConfigureServices中正确注册:

public void ConfigureServices(IServiceCollection services) {
    services.AddScoped<IDocumentRepository, DocumentRepository>();
}