context.Database.ExecuteSql raise the title exception. How must I use raw sql in .net core to retrieve a dataset and process it like ?
public async Task<IActionResult> Refresh_Formatos()
{
using (var context = _context)
{
var padre = context.Database.SqlQuery<int>(
$@"SELECT id
FROM pds_etiquetas where nombre='FORMATOS' and tipo = 'Cat'
").ToList();
int padre_id = padre[0];
var formatos = context.Database.SqlQuery<string>(
$@"SELECT xdescripcion
FROM vc00_formatos a
left join pds_etiquetas c on a.xdescripcion=c.Nombre and {padre_id}=c.PadreId and c.tipo = 'Val'
where c.id is null
order by xdescripcion");
context.Database.CloseConnection();
foreach (var formato in formatos)
{
var affectedRows = context.Database.ExecuteSql(
$@"INSERT INTO [imp].[pds_etiquetas]
([Nombre]
,[Tipo]
,[PadreId])
VALUES
('{formato}'
,'Val'
,{padre_id})
");
}
}
return Ok();
}
You need to put
formatosinto aListAsyncfunctions, but it appears the old EF does not provideSqlQueryAsync.Having said that, you don't need to do any of this, you can just do one big joined
INSERT.You can also change the
LEFT JOINto aNOT EXISTS, and you should use a new context rather than an existing one.