Creating a new project
Create a new project in Visual Studio 2017/2019.
Select ASP.NET Core Web Application and select API template as below.
Create a new folder in Solution Explorer and name it as "Models" as following.
Add a new class in the Models folder by right-clicking on folder > Add > Class.
Name your class as Customer and add the following code in the class.
- public class Customer
- {
- [Key]
- [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
- public int ID { get; set; }
- public string Name { get; set; }
- public string Email { get; set; }
- public string Mobile { get; set; }
- }
For implementing Generic Repository, we have to add an interface to define our generic functions.
Right-click on Models class and add an interface, name it as "IDataRepository", and add the following code.
- public interface IDataRepository<TEntity>
- {
- IEnumerable<TEntity> GetAll();
- TEntity Get(int id);
- void Add(TEntity entity);
- void Change(TEntity dbEntity, TEntity entity);
- void Delete(TEntity entity);
- }
We have declared five methods for CRUD operations and these methods will be implemented in action classes of every model.
Let's add an Actions class. Right-click on the Models folder and add a class name it as "CustomerManager" and add the following code in the class.
- public class CustomerManager : IDataRepository<Customer>
- {
- readonly ResturantDbContext _resturantDb;
- public CustomerManager(ResturantDbContext context)
- {
- _resturantDb = context;
- }
-
- public IEnumerable<Customer> GetAll()
- {
- return _resturantDb.Customers.ToList();
- }
-
- public Customer Get(int id)
- {
- return _resturantDb.Customers
- .FirstOrDefault(e => e.ID == id);
- }
-
- public void Add(Customer customer)
- {
- _resturantDb.Customers.Add(customer);
- _resturantDb.SaveChanges();
- }
-
- public void Change(Customer customer, Customer entity)
- {
-
- customer.Name = entity.Name;
- customer.Email = entity.Email;
- customer.Mobile = entity.Mobile;
-
- _resturantDb.SaveChanges();
- }
-
- public void Delete(Customer customer)
- {
- _resturantDb.Customers.Remove(customer);
- _resturantDb.SaveChanges();
- }
- }
The CustomerManager class in inherited by the IDataRepository interface and all the methods that we have defined in IDataRepository are implemented in this class for CRUD operations.
Now, let's add a class as our DbContext class. Right-click on the Models folder and add a new class.
Name it as ResturantDbContext and add the following code.
- public class ResturantDbContext:DbContext
- {
- public ResturantDbContext(DbContextOptions<ResturantDbContext> options)
- : base(options)
- {
-
- }
-
- public DbSet<Customer> Customers { get; set; }
- }
ResturantDbContext is inherited by DbContext class of EntityFrameworkCore. Add a new migration by witing the following command in Package-Manager Console.
- add-migration CustomerTable
Press Enter. A new migration will be added to your Migrations folder. (If you do not have a Migrations folder, it will be added automatically), as below.
Add the connection string as following in your appsetting.json file.
Run the following command in your Package-Manager Console to update the database.
By running the above command, the database will be generated and the table will be created in your database.
Now, for implementing CRUD operations, we have to add a Controller for Customer Class.
Right click on the Controllers folder and add a New Controller.
Name it as CustomersController and add the following code into it.
- public class CustomersController : Controller
- {
- private IDataRepository<Customer> _repository;
- public CustomersController (IDataRepository<Customer> repository)
- {
- _repository = repository;
- }
-
-
- [HttpGet]
- public IActionResult Get()
- {
- IEnumerable<Customer> customers = _repository.GetAll();
- return Ok(customers);
- }
-
-
- [HttpGet("{id}")]
- public IActionResult Get(int id)
- {
- Customer customer = _repository.Get(id);
-
- if (customer == null)
- {
- return NotFound("The Employee record couldn't be found.");
- }
-
- return Ok(customer);
- }
-
-
- [HttpPost]
- public IActionResult Post([FromBody]Customer customer)
- {
- if (customer == null)
- {
- return BadRequest("Customer is Null");
- }
- _repository.Add(customer);
- return CreatedAtRoute("Get", new { Id = customer.ID }, customer);
- }
-
-
- [HttpPut("{id}")]
- public IActionResult Put(int id, [FromBody]Customer customer)
- {
- if (customer == null)
- {
- return BadRequest("Customer is null");
- }
- Customer customerToUpdate = _repository.Get(id);
- if (customerToUpdate == null)
- {
- return NotFound("Customer could not be found");
- }
- _repository.Change(customerToUpdate, customer);
- return NoContent();
- }
-
-
- [HttpDelete("{id}")]
- public IActionResult Delete(int id)
- {
- Customer customer = _repository.Get(id);
- if (customer == null)
- {
- return BadRequest("Customer is not found");
- }
- _repository.Delete(customer);
- return NoContent();
- }
- }
In this controller, we declared the instance of repository in line 3 and in <> braces, we entered customer to make it understand that all
methods will be used for Customer class.
Add one or two entries in your database table.
Run your project.
Handle the Get Request using POSTMAN.
Here is the result of our GET request.
For handling all four CRUD requests using POSTMAN, you can use
this link.
Happy Coding!