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