I am using Entity Framework Core and when I run the following query it all works as expected and selects all entities from the flasher_equipment
table.
public async Task<IEnumerable<BaseEquipmentType>> GetNewAvailableEquipment(string tableName)
{
DbSet<BaseEquipmentType> dbSet = _context.Set<BaseEquipmentType>();
IQueryable<BaseEquipmentType> types = dbSet.FromSql($"select * from flasher_equipment");
return await types.ToArrayAsync();
}
but now, instead of hard coding the table name (flasher_equipment
) I want to pass it as a parameter.
I've tried changing the code as follows:
public async Task<IEnumerable<BaseEquipmentType>> GetNewAvailableEquipment(string tableName)
{
DbSet<BaseEquipmentType> dbSet = _context.Set<BaseEquipmentType>();
IQueryable<BaseEquipmentType> types = dbSet.FromSql($"select * from {tableName}");
return await types.ToArrayAsync();
}
and I've also tried
public async Task<IEnumerable<BaseEquipmentType>> GetNewAvailableEquipment(string tableName)
{
DbSet<BaseEquipmentType> dbSet = _context.Set<BaseEquipmentType>();
IQueryable<BaseEquipmentType> types = dbSet.FromSql("select * from {0}", tableName);
return await types.ToArrayAsync();
}
Each time I get an error:
Grpc.AspNetCore.Server.ServerCallHandler[6]
Error when executing service method 'GetByPlanIdAnImplementation'. Oracle.ManagedDataAccess.Client.OracleException (0x80004005): ORA-00903: invalid table name
Why is parameterizing the table name as a parameter causing it to crash?
Seems to be an problem of the FromSql
method with the string interpolation.
Probably it will work if you interpolate the string outside the method, as:
public async Task<IEnumerable<BaseEquipmentType>> GetNewAvailableEquipment(string tableName)
{
DbSet<BaseEquipmentType> dbSet = _context.Set<BaseEquipmentType>();
string sqlStatement = $"select * from {tableName}";
IQueryable<BaseEquipmentType> types = dbSet.FromSql(sqlStatement);
return await types.ToArrayAsync();
}
User contributions licensed under CC BY-SA 3.0