私は自分自身にC#を教えて、Entity Framework Coreとリポジトリーパターンを使って遊んでいます。私は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 .htm。そうでなければ、それはコントローラでそれを使う方法をカバーします – sr28