Hello !
I have incorrect behavior. When I switch from one dbcontext to another, in my source code, step 1: var userFiltres = await _userFiltreExploitantRepository.GetByUserIdAsync(userId.Value); it retrieves the objects correctly, but in step 2, it doesn't retrieve the objects, saying it can't find the stored procedure located in the EA_ProdbContext. However, if I swap step 2 and step 1, it will retrieve the objects correctly, but won't find the UserFiltreExploitant table, which is located in the Portail_GTCDbContext. Switching the dbcontext: EA_ProdbContext and Portail_GTCDbContext doesn't seem to take the dbcontext change into account.
`public class EAProdAppService : Portail_GTCAppServiceBase, IEAProdAppService { private readonly IControleEARepository _controleEARepository; private readonly IUserFiltreExploitantRepository _userFiltreExploitantRepository;
public EAProdAppService(IControleEARepository controleEARepository,
IUserFiltreExploitantRepository userFiltreExploitantRepository)
{
_userFiltreExploitantRepository = userFiltreExploitantRepository;
_controleEARepository = controleEARepository;
}
public virtual async Task<PagedResultDto<GetControleEAForViewDto>> GetAllControleEA(ControleEAInput input)
{
var userId = AbpSession.UserId;
// 1. Récupérer les FiltreExploitantId de l'utilisateur connecté
List<long> userFiltreIds = new List<long>();
if (userId.HasValue)
{
var userFiltres = await _userFiltreExploitantRepository.GetByUserIdAsync(userId.Value); <======= OK
userFiltreIds = userFiltres.Select(f => f.FiltreExploitantId).ToList();
}
// 2. Récupérer tous les ControleEA
var allControleEA = await _controleEARepository.GetControlesAsync(); <============ NO OK
var filteredControleEA = allControleEA.AsQueryable();
// 3. Filtrage par exploitants de l'utilisateur connecté
if (userFiltreIds.Any())
{
var userFiltreIdsAsString = userFiltreIds.Select(id => id.ToString()).ToList();
filteredControleEA = filteredControleEA
.Where(e => !string.IsNullOrEmpty(e.NumExploitant) &&
userFiltreIdsAsString.Contains(e.NumExploitant));
}
else
{
// Aucun exploitant assigné => retourne vide
filteredControleEA = filteredControleEA.Where(e => false);
}
`
`namespace Portail_GTC.UserFiltreExploitants { public class UserFiltreExploitantRepository : Portail_GTCRepositoryBase<UserFiltreExploitant, long>, IUserFiltreExploitantRepository { private readonly IDbContextProvider<Portail_GTCDbContext> _dbContextProvider;
public UserFiltreExploitantRepository(IDbContextProvider<Portail_GTCDbContext> dbContextProvider)
: base(dbContextProvider)
{
_dbContextProvider = dbContextProvider ?? throw new ArgumentNullException(nameof(dbContextProvider));
}
public async Task<List<UserFiltreExploitant>> GetByUserIdAsync(long userId)
{
try
{
var context = await _dbContextProvider.GetDbContextAsync();
return await context.UserFiltresExploitants.Where(u => u.UserId == userId).ToListAsync();
}
catch (Exception ex)
{
throw;
}
}
}
}
``namespace Portail_GTC.repository { public class ControleEARepository : EA_ProdbRepositoryBase
public ControleEARepository(IDbContextProvider<EA_ProdbContext> dbContextProvider)
: base(dbContextProvider)
{
_dbContextProvider = dbContextProvider ?? throw new ArgumentNullException(nameof(dbContextProvider));
}
public async Task<IQueryable<ControleEA>> GetControlesAsync()
{
try
{
// parameters
var parameters = new[]
{
new SqlParameter("@Exploitant", -1),
new SqlParameter("@Constructeur", -1),
new SqlParameter("@NumLot", DBNull.Value),
new SqlParameter("@NumExploitant", DBNull.Value),
new SqlParameter("@NumConstructeur", DBNull.Value),
new SqlParameter("@Nomclient", DBNull.Value),
new SqlParameter("@CP", DBNull.Value),
new SqlParameter("@Prestataire", -1),
new SqlParameter("@Debut", new DateTime(2024, 08, 31)),
new SqlParameter("@Fin", new DateTime(2024, 12, 31)),
new SqlParameter("@Classe", DBNull.Value),
new SqlParameter("@Approuve", 1),
new SqlParameter("@DateAllSearch", 3)
};
// Call the stored procedure directly from the context
var context = await _dbContextProvider.GetDbContextAsync();
var result = await context.ControleEAs // Your DbSet name might be different
.FromSqlRaw("EXEC wb_ControlesRT02 @Exploitant, @Constructeur, @NumLot, @NumExploitant, @NumConstructeur, @Nomclient, @CP, @Prestataire, @Debut, @Fin, @Classe, @Approuve, @DateAllSearch", parameters)
.ToListAsync();
return result.AsQueryable();
}
catch (Exception ex)
{
throw;
}
}
}
}`