Create an ASP .NET App that enables CRUD Operation for the Customer Database.
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string? Address { get; set; }
public string? Phone { get; set; }
}
CustomerData
CustomerWeb
Import the following namespaces:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
Under namespace CustomerData.Models
, create a public abstract class BaseEntity
public abstract class BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
}
Under namespace CustomerData.Models
, create a public class Customer
public class Customer : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string? Address { get; set; }
public string? Phone { get; set; }
}
Import the following namespaces:
using CustomerData.Models;
using Microsoft.EntityFrameworkCore;
Create a public class CustomerDbContext
that derives from DbContext
Class
public class CustomerDbContext : DbContext
{
private readonly string _connectionString;
public CustomerDbContext() : this(
"Server = DESKTOP-47CLFBT; " +
"Database = CustomerDb; " +
"User Id = sa; " +
"Password = D@gisik@n1997*")
{
}
public CustomerDbContext(string connectionString)
{
this._connectionString = connectionString;
}
public DbSet<Customer> Customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(this._connectionString);
}
}
}
Through the Package Manager Console
, Enter Add-Migration [Insert any name]
command to create Migrations
folder.
Then, use the command Update-Database
to update the Database.
Create a Repository
Folder that creates an abstraction layer between the data access layer and the business logic layer of an application.
Create a GenericRepository
and a CustomerRepository
class under the Repository
folder
using CustomerData.Context;
using CustomerData.Models;
using Microsoft.EntityFrameworkCore;
public interface IBaseRepository<T> where T : BaseEntity
{
IEnumerable<T> FindAll();
T FindByPrimaryKey(int id);
T Insert(T entity);
T Update(T entity);
T Delete(int id);
}
public class GenericRepository<T> where T : BaseEntity
{
}
Under the GenericRepository<T>
class, create the constructor with the parameter CustomerDbContext context
public GenericRepository(CustomerDbContext context)
{
this.Context = context;
}
public CustomerDbContext Context { get; set; }
FindAll()
Displays all data from the database.
public IEnumerable<T> FindAll()
{
IQueryable<T> query = this.Context.Set<T>();
return query.Select(c => c).ToList();
}
FindByPrimaryKid(int id)
Looks for a data in the database given an id.
public T FindByPrimaryKey(int id)
{
var entity = this.Context.Find<T>(id);
if (entity is object)
{
this.Context.Entry<T>(entity).State = EntityState.Detached;
return entity;
}
throw new Exception($"Entity with ID {id} was not found.");
}
Insert(T entity)
Inserts a data to the database.
public T Insert(T entity)
{
Context.Add<T>(entity);
Context.SaveChanges();
return entity;
}
Update(T entity)
Updates a data from the database.
public T Update(T entity)
{
this.Context.Attach<T>(entity);
this.Context.Entry(entity).State = EntityState.Modified;
this.Context.SaveChanges();
return entity;
}
Delete(int id)
Deletes data from the database.
public T Delete(int id)
{
var entity = this.FindByPrimaryKey(id);
this.Context.Remove<T>(entity);
this.Context.SaveChanges();
return entity;
}