using ApiPolo.Data; using ApiPolo.Interfaces; using ApiPolo.Models; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Internal; using static ApiPolo.Controllers.PoloController; namespace ApiPolo { /// public class Service { //private readonly Maras_DbContext _dbContext; private readonly ITenantDbContextFactory _dbContextFactory; // Constructor injection of DbContext /// public Service( ITenantDbContextFactory dbContextFactory) { //_dbContext = dbContext; _dbContextFactory = dbContextFactory; } // Example method /// //public async Task AddRappNewToDbAsync2(string tenant, Rapp_New model) //{ // // Some logic to add entity to DbContext // //await _dbContext.Rapps.AddAsync(model); // //await _dbContext.SaveChangesAsync(); // if (tenant.Equals(Clienti.Maras)) // { // using (var transaction = _dbContext.Database.BeginTransaction()) // { // await _dbContext.Rapps.AddAsync(model); // await _dbContext.SaveChangesAsync(); // transaction.Commit(); // } // } // //else if (dbContext is OtherDbContext otherDbContext) // //{ // // await otherDbContext.AddAsync(entity); // // await otherDbContext.SaveChangesAsync(); // //} // else // { // throw new InvalidOperationException("Unsupported DbContext type."); // } //} /// public async Task AddRappNewToDbAsync(string tenant, Rapp_New model) { var dbContext = _dbContextFactory.GetDbContext(tenant); string mex = string.Empty; try { using (var transaction = await dbContext.Database.BeginTransactionAsync()) { await dbContext.Rapps.AddAsync(model); await dbContext.SaveChangesAsync(); await transaction.CommitAsync(); } } catch (Exception ex) { mex = ex.Message; throw new InvalidOperationException("Error in Service: "+mex); } } } }