我在教自己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.
可以请别人帮忙= - 说白了,我不报价把握所有技术术语
你能解释一下哪些工作不正常吗? – sr28
那么我现在需要将接口实现为一个控制器 - 我不知道该怎么做。 – Webezine
您是否有机会使用本教程:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and-entity-framework热媒。如果没有,那么它涵盖了如何在控制器中使用它 – sr28