0

我在教自己c#锐利和玩弄实体框架核心和存储库模式。我已经设法让EFcore正常工作,从本地的sql储存等工具中获取信息。我现在试图通过一个存储库来获得这个工作。 我创建了一个IrepositoryFile和信息库为每个方法:实施与控制器的存储库

public interface ICustomerRepository 
{ 
    IEnumerable<Customer> GetCustomers(); 
    Customer GetCustomerById(int customerId); 
    void InsertCustomer(Customer customer); 
    void DeleteCustomer(int customerId); 
} 
public class CustomerRepository : ICustomerRepository 
{ 
    private masterContext context; 

    public IEnumerable<Customer> GetCustomers() 
    { 
     return context.Customer.ToList(); 
    } 

    public void InsertCustomer(Customer customer) 
    { 
     context.Customer.Add(customer); 
     context.SaveChanges(); 
    } 

    public void DeleteCustomer(int customerId) 
    { 
     //Customer c = context.Customer.Find(customerID); 
     var cc = context.Customer.Where(ii => ii.CustomerId == customerId); 
     context.Remove(cc); 
     context.SaveChanges(); 
    } 

    public Customer GetCustomerById(int customerId) 
    { 
     var result = (from c in context.Customer where c.CustomerId == customerId select c).FirstOrDefault(); 
     return result; 
    } 
} 

我现在努力得到它的工作,并采取投入控制器,这显示在HTML页面上的下一个步骤。

这是我通过控制器实现仓库的尝试:

using System.Collections.Generic; 
using CustomerDatabase.Core.Interface; 
using CustomerDatabase.Core.Models; 
using Microsoft.AspNetCore.Mvc; 

namespace CustomerDatabase.Core.Controllers 
{ 
    public class CustomerController2 : Controller 
    { 
     private readonly ICustomerRepository _repository = null; 
     public CustomerController2() 
     { 
      this._repository = new CustomerRepository(); 
     } 
     public CustomerController2(ICustomerRepository repository) 
     { 
      this._repository = repository; 
     } 

     public ActionResult Index() 
     { 
      List<Customer> model = (List<Customer>)_repository.GetCustomers(); 
      return View(model); 
     } 

     public ActionResult New() 
     { 
      return View(); 
     } 

     public ActionResult Insert(Customer obj) 
     { 
      _repository.InsertCustomer(obj); 
      _repository.Save(); 
      return View(); 
     } 

     public ActionResult Edit(int id) 
     { 
      Customer existing = _repository.GetCustomerById(id); 
      return View(existing); 
     } 

    } 
} 

,但我得到这个错误:

Multiple constructors accepting all given argument types have been found in type 'CustomerDatabase .Core. Controllers. CustomerController. There should only be one applicable constructor. 

可以请别人帮忙= - 说白了,我不报价把握所有技术术语

+1

你能解释一下哪些工作不正常吗? – sr28

+0

那么我现在需要将接口实现为一个控制器 - 我不知道该怎么做。 – Webezine

+0

您是否有机会使用本教程:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and-entity-framework热媒。如果没有,那么它涵盖了如何在控制器中使用它 – sr28

回答

1

我认为你的问题在于这两个构造函数:

public CustomerController2() 
{ 
    this._repository = new CustomerRepository(); 
} 

public CustomerController2(ICustomerRepository repository) 
{ 
    this._repository = repository; 
} 

从一点点阅读它看起来像内置解析器不支持暴露多个构造函数的类型。请参阅this linked question

+0

对不起,我知道我在这里要求一些手,但我会如何重写?我不太了解如何... – Webezine

+1

@ user3223507 - 没有通读整篇教程,你正在关注并试图准确理解它在做什么,这有点棘手。然而,我最好的猜测是你只需要删除1个构造函数,很可能是无参数构造函数,因为这实际上是一个默认的构造函数。 – sr28

+0

这似乎工作 - 至少它让我进一步通过代码,但现在又是一个错误。非常感谢你! – Webezine