Before going to problem my Folder structure is something like
Actually I'm trying to inject the DBContext from startup to controllers.
startup code:
services.AddScoped<DbContext>(_ => new DAL.DbContext(Configuration.GetConnectionString("DefaultConnection")));
In controller constructor:
private UnitOfWork _unitOfWork;
private DbContext _context;
private ProfileManager _ProfileManager ;
public ProfileController( DbContext context)
{ _context = context;
_unitOfWork = new UnitOfWork(_context);
_ProfileManager = new ProfileManager(_unitOfWork, _context);
}
[HttpGet("[action]")]
public List<ProfileRole> GetAllProfileRoles()
{
try
{
var list= _ProfileManager.GetAllProfileRoles().ToList();
return list;
}
catch (Exception exception)
{
throw exception;
}
}
Unitofwork class:
public UnitOfWork(DbContext dbContext)
{
_dbContext = dbContext;
}
Manager class:
public ProfileManager(IUnitOfWork unitofwork, DbContext context)
{
_context = context;
_unitOfWork = unitofwork;
}
public List<ProfileRole> GetAllProfileRoles()
{
try
{
var listofrole=(from role in _unitOfWork.Roles.GetAll()
join profilerole in _unitOfWork.ProfileRoles.GetAll()
on role.Id equals profilerole.Role.Id
join profile in _unitOfWork.Profiles.GetAll()
on profilerole.profile.Id equals profile.Id
select new {
profilename=profile.Name
RoleName=role.Name
}).ToList();
return listofrole;
}
catch (Exception ex)
{
throw ex;
}
}
Dbcontext class:
public DbContext(string connectionString) : base(connectionString)
{
}
actually problem is when there is multiple call to GetAllProfileRoles() from controller I'm facing lot of issues regarding to Entity Framework
I've gone through SO solution but none of the ans solved my issues
tried method:
but what I understand is Dbcontext is created per request so if there is two request then I'm getting this error, but I'm failed to handle multiple request.
User contributions licensed under CC BY-SA 3.0