0

私は自分自身に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. 

誰かが=助けることができますしてください - 私はすべて引用把握いけないとはっきり話します技術用語

+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 .htm。そうでなければ、それはコントローラでそれを使う方法をカバーします – sr28

答えて

1

あなたの問題は、次の2つのコンストラクタにあると思います。

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

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

読んだところから、組み込みリゾルバは複数のコンストラクタを公開する型をサポートしていないようです。 this linked questionを参照してください。

+0

申し訳ありません私はここに手を置くことを少し求めていることを知っていますが、私はこれをどのように書き直すのでしょうか?私はかなりの方法を把握していません... – Webezine

+1

@ user3223507 - あなたのチュートリアル全体を読んでいなくても、それが何をしているのかをちょっと分かりやすくしようとしています。しかし、私の推測では、コンストラクタのうちの1つを削除するだけでよく、おそらくそれが実質的にデフォルトのコンストラクタであるため、パラメータなしのコンストラクタを削除する必要があります。 – sr28

+0

それはうまくいっているようです - 少なくとも私はコードをさらに進めましたが、今は別のエラーです。どうもありがとうございました! – Webezine

関連する問題