在上下文中没有给出任何论据

时间:2016-08-24 12:03:16

标签: c# asp.net-mvc entity-framework asp.net-core entity-framework-core

我正在尝试在asp core中实施repository pattern。除了将一些调整添加到控制器之外,一切似乎都能正常工作:

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

错误

  

没有任何论据符合所要求的形式   参数'选项'   “myContext.myContext(DbContextOptions)   dotnetcore..NETCoreApp,版本= 1.0

1 个答案:

答案 0 :(得分:2)

只需使用构造函数注入从DI容器中解析SELECT [Lieferant] ,ISNULL([ok], 0) AS [ok] ,ISNULL([W180], 0) AS [W180] ,ISNULL([W360], 0) AS [W360] ,ISNULL([W720], 0) AS [W720] FROM ( SELECT [Lieferant] ,[Warnstufe] AS [AnzahlvonWarnstufe] FROM [dbo].[VIEW_qTeile-LF-Beziehungen_zuTeileImportListe_PLUS_Warnstufe] ) src pivot ( COUNT([AnzahlvonWarnstufe]) FOR [AnzahlvonWarnstufe] IN ([ok], [W180], [W360], [W720]) ) piv; ,而不是手动实例化它,它应该可以工作:

IDocumentRepository

为此,您需要确保在public class HomeController : Controller { private IDocumentRepository _repository; public HomeController(IDocumentRepository repository) { _repository = repository; } } 中正确注册IDocumentRepository

ConfigureServices