Author : MD TAREQ HASSAN

MSSQLLocalDB

For simplicity, gonna use MSSQLLocalDB

Create database in MSSQLLocalDB

EF core data project

See: modify the DbContext class so that connection string can be configured in Startup.ConfigureServices()

ASP.Net core project

Connection string in appsettings appsettings.json

{

  "ConnectionStrings": {
    "EmployeeDBConnection": "Server=(localdb)\\mssqllocaldb;Database=EmployeeDB;AttachDbFileName=%CONTENTROOTPATH%\\AppData\\EmployeeDB.mdf;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  
  
}

Notes:

DataUtils

public static class DataUtils
{

	public const string CONNECTION_STRING_KEY = "EmployeeDBConnection";

	public const string CONTENT_ROOT_PLACE_HOLDER = "%CONTENTROOTPATH%";

	public static string ResolveDbConnectionString(IConfiguration Configuration, string contentRootPath, string connectionStringKey = CONNECTION_STRING_KEY) { 
	
		var connectionString = Configuration.GetConnectionString(connectionStringKey);

		if (connectionString.Contains(CONTENT_ROOT_PLACE_HOLDER))
		{
			connectionString = connectionString.Replace(CONTENT_ROOT_PLACE_HOLDER, contentRootPath);
		}

		return connectionString;
	}
}

Repository

IEmployeeRepository.cs

public interface IEmployeeRepository
{
	long Create(Employee newEntity);
	Employee Read(long id);
	void Update(Employee modifiedEntity);
	void Delete(Employee entityToDelete);
}

EmployeeRepository.cs

public class EmployeeRepository : IEmployeeRepository
{

	private readonly EmployeeDbContext _context;

	public EmployeeRepository(EmployeeDbContext context) // Constructor DI Injection
	{
		_context = context;
	}

	public long Create(Employee newEmployee)
	{
		_context.Employees.Add(newEmployee);
		_context.SaveChanges();

		return newEmployee.Id;
	}
	
	public Employee Read(long id)
	{
		return _context.Employees.Find(id);
	}

	public void Update(Employee employeeToUpdate)
	{
		_context.Employees.Update(employeeToUpdate);
		_context.SaveChanges();
	}

	public void Delete(Employee employeeToDelete)
	{
		_context.Employees.Remove(employeeToDelete);  //_context.Remove<Employee>(new Employee { Id = id });
		_context.SaveChanges();
	}
}

DI injection setup

Startup.cs (in ASP.Net core project)

public class Startup
{
	public IConfiguration Configuration { get; }
	private readonly string _contentRootPath;

	public Startup(IConfiguration configuration, IHostingEnvironment env)
	{
		Configuration = configuration;
		_contentRootPath = env.ContentRootPath;
	}

	public void ConfigureServices(IServiceCollection services)
	{
		var dbConnectionString = DataUtils.ResolveDbConnectionString(Configuration, _contentRootPath);
		services.AddDbContextPool<EmployeeDbContext>(options => options.UseSqlServer(dbConnectionString));

		services.AddScoped<IEmployeeRepository, EmployeeRepository>();
		services.AddScoped<IEmployeeService, EmployeeService>();
		
		services.AddAutoMapper(typeof(Startup));

		// ... ... ...

	}
	
	// ... ... ...
}

Using repository

EmployeeService.cs

public class EmployeeService : IEmployeeService
{
	private readonly IEmployeeRepository _employeeRepository;
	private readonly IMapper _autoMapper;

	public EmployeeService(IEmployeeRepository employeeRepository, IMapper autoMapper)
	{
		_employeeRepository = employeeRepository;
		_autoMapper = autoMapper;
	}
	
	// ... ... ...
}