0

I'm teaching myself c#sharp and playing around with Entity Framework Core and the repository pattern. I've managed to get EFcore working fine pulling info from a local sql saving etc.. I'm now attempting to get this working via a repository. I've created an IrepositoryFile and Repository for each method :

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

I'm now struggling getting it to work and taking the next steps of putting this into a controller to show on the html page.

This is my attempt of implementing the repository through the controller:

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

    }
}

But i get this error:

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

PLEASE can someone help =- speak plainly as I dont quote grasp all the tech terms

Webezine
  • 345
  • 7
  • 22
  • 1
    Can you explain exactly what's not working? – sr28 Aug 18 '16 at 13:41
  • Well I now need to implement the interface into a controller - and I don't know how / what to do. – Webezine Aug 18 '16 at 13:59
  • Are you by any chance using this tutorial: http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and-entity-framework.htm. If not, then it covers how to use it in a controller – sr28 Aug 18 '16 at 14:07
  • Yep trying to do that and followed their example but im having problems - amended above to give more info. – Webezine Aug 18 '16 at 14:09
  • Can you show CustomerController please? – sr28 Aug 18 '16 at 14:17
  • Edited original post to include – Webezine Aug 18 '16 at 14:25
  • 1
    Error message mention `CustomerDatabase.Core.Controllers.CustomerController`, while you provide code for `CustomerDatabase.Core.Controllers.CustomerController2`. Can't debug absent code. – Vadim Levkovsky Aug 18 '16 at 14:29

1 Answers1

1

I think your problem lies with these 2 constructors:

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

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

From a little bit of reading it looks like the built in resolver doesn't support types that expose multiple constructors. See this linked question.

Community
  • 1
  • 1
sr28
  • 4,728
  • 5
  • 36
  • 67
  • Sorry I know I'm asking for a bit of hand holding here but how would I re-write this? I don't quite grasp how to... – Webezine Aug 18 '16 at 14:46
  • 1
    @user3223507 - well without reading through your whole tutorial that you're following and trying to understand exactly what it's doing it's a bit tricky. However, my best guess would be that you just need to delete 1 of the constructors, most likely the parameter-less one as that is effectively a default constructor. – sr28 Aug 18 '16 at 14:51
  • That seems to have worked - at least its got me further through the code but another error now. Thank you very much! – Webezine Aug 18 '16 at 14:53
  • @user3223507 - if this has worked please mark it as the answer (click the tick next to the tick next to the answer as well). Thanks. – sr28 Aug 18 '16 at 14:57