EF Core - how properly manage connection with repostory pattern and oracle procedure

Viewed 29

I'm starting using entity framework with oracle database, since i have to call oracle package that return refcurson i'am wondering if there is a way to let ef manage connection, transaction and so on ora if I have to mess with it

this is my code

var comando = _context.Database.GetDbConnection().CreateCommand();
    await _context.Database.OpenConnectionAsync(); // can avoid?

    comando.CommandText = $"PKG_XXX.YYY";
    comando.CommandType = CommandType.StoredProcedure;

    comando.Parameters.Add(new OracleParameter("p1", 1));

    using var reader = comando.ExecuteReaderAsync().Result;

    IEnumerable<DtoElencoFlussi> _return;
    var dt = new DataTable();
    dt.Load(reader);
    IEnumerable<DataRow> rows = dt.AsEnumerable();
    _return =  rows.Select(dr => new DtoElencoFlussi()
    {
        Id = dr[0].ToString(),
        Descrizione = dr[1].ToString(),         
    }).ToList();
    
    _context.Database.CloseConnection();  // can avoid?

    return _return;

now i'm forced to manually open connection since found it close, and so i have to close it and i will need to add code to manage command error

is there a way to avoid it?

automaticcaly open it in unitowwork creation since not a good way since standard DbSet does not require explicit connection opening

my context

public DatabaseContext(DbContextOptions<DatabaseContext> options)
 : base(options)     {    }

public Task<int> SaveChangesAsync() => base.SaveChangesAsync();

and UoW

 private readonly DatabaseContext _context;
    public UnitOfWork(DatabaseContext context)
    {
        _context = context;
        ParametriConfigurazione = new ParametroConfigurazioneRepository(_context);
        Elenchi = new ElenchiRepository(_context);
    }

public async Task<int> SaveAllAsync() => await _context.SaveChangesAsync();
public async Task DiscardAllAsync() => await _context.DisposeAsync();
0 Answers
Related