How to handle multiple HTTP request to same method at same time in EF c#

-2

Before going to problem my Folder structure is something like

  1. DAL(where unitofwork+ DBcontext class+entities for EF code first)
  2. Business layer (repo+Manager)
  3. view(startup,program,angular 4)

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

  1. 'The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.'
  2. System.Data.Entity.Core.EntityException
    HResult=0x80131501
    Message=The underlying provider failed on Open.

I've gone through SO solution but none of the ans solved my issues

tried method:

  1. I try to make transient instead of scope
  2. If I make inline query instead of lambda ,in rare case I'm getting error but its not an solution

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.

c#
entity-framework
asp.net-core
asked on Stack Overflow Dec 6, 2018 by user2351143 • edited Jan 3, 2019 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0