.Net Core : Database Connectivity
Model
namespace DotNetCodeDemo.Entities
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Department Department { get; set; }
}
}
namespace DotNetCodeDemo.Entities
{
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
}
Repository
namespace DotNetCodeDemo.Repository.Contract
{
public interface IEmployeeRepository
{
Employee GetEmployee(int Id);
IEnumerable<Employee> GetAllEmployee();
Employee Add(Employee employee);
Employee Update(Employee employeeChanges);
Employee Delete(int Id);
}
}
namespace DotNetCodeDemo.Repository.Repositories
{
public class EmployeeRepository : IEmployeeRepository
{
private readonly AppDbContext dbContext;
public EmployeeRepository(AppDbContext dbContext)
{
this.dbContext = dbContext;
}
public Employee Add(Employee employee)
{
dbContext.Employees.Add(employee);
dbContext.SaveChanges();
return employee;
}
public Employee Delete(int Id)
{
var employee = dbContext.Employees.Find(Id);
if(employee != null)
{
dbContext.Employees.Remove(employee);
dbContext.SaveChanges();
}
return employee;
}
public IEnumerable<Employee> GetAllEmployee()
{
return dbContext.Employees;
}
public Employee GetEmployee(int Id)
{
return dbContext.Employees.Find(Id);
}
public Employee Update(Employee employeeChanges)
{
var employee = dbContext.Employees.Attach(employeeChanges);
employee.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
dbContext.SaveChanges();
return employeeChanges;
}
}
}
namespace DotNetCodeDemo.Repository.Helper
{
public static class ModelBuilderExtension
{
public static void Seed(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().HasData(
new Employee
{
Id = 1,
Name = "Demo1",
Email = "demo1@demo.com"
},
new Employee
{
Id = 2,
Name = "Demo2",
Email = "demo2@demo.com"
}
);
}
}
}
namespace DotNetCodeDemo.Repository
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Seed();
}
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DotNetCoreDB" : "data source=localhost; initial catalog=dotnetCoreDb; user id=sa; password=sa"
}
}
Startup.cs
namespace DotNetCodeDemo
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DotNetCoreDB")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddScoped<IEmployeeRepository, EmployeeRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
API Controller
namespace DotNetCodeDemo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
private readonly IEmployeeRepository employeeRepository;
public EmployeeController(IEmployeeRepository employeeRepository)
{
this.employeeRepository = employeeRepository;
}
[HttpGet]
public ActionResult<IEnumerable<Employee>> Get()
{
return employeeRepository.GetAllEmployee().ToList();
}
}
}
Hope this will help you and save your time.
Enjoy !!!
:)
No comments:
Post a Comment