英文:
EF Core 8 preview version raw SQL queries for unmapped types
问题
We have to call the stored procedure from .NET 6 API using raw SQL in EF Core 8 preview. It was working fine if all the properties which have public sets in the SQL query ie stored procedure.
But in a few scenarios, we want to pull only a sub-set of columns, but still want to use the same model. However, in this case, an error was thrown:
> The required column 'colname' was not present in the results of a 'FromSql' operation, but we want to solve without all public properties present in the query.
This is my method:
public IEnumerable<T> ExecStoredProc<T>(string procName,IDictionary<string, object> parameters)
{
try
{
var inputParameter = new List<SqlParameter>(parameters.Count);
foreach (var parameter in parameters)
{
inputParameter.Add(new SqlParameter
{
ParameterName = parameter.Key,
Value = parameter.Value ?? DBNull.Value
});
}
string procWithParameter = string.Format("exec {0} {1}", procName, string.Join(", ", inputParameter.Select(p => p.ParameterName)));
var result = _dbContext.Database.SqlQueryRaw<T> (procWithParameter, inputParameter.ToArray());
return result.ToList();
//return result.ToList();
}
catch (System.Exception ex)
{
throw ex;
}
}
英文:
We have to call the stored procedure from .NET 6 API using raw SQL in EF Core 8 preview. It was working fine if all the properties which have public sets in the SQL query ie stored procedure.
But in a few scenarios, we want to pull only a sub-set of columns, but still want to use the same model. However, in this case, an error was thrown:
> The required column 'colname' was not present in the results of a 'FromSql' operation.but we want to solve without all public properties present in the query.
This is my method:
public IEnumerable<T> ExecStoredProc<T>(string procName,IDictionary<string, object> parameters)
{
try
{
var inputParameter = new List<SqlParameter>(parameters.Count);
foreach (var parameter in parameters)
{
inputParameter.Add(new SqlParameter
{
ParameterName = parameter.Key,
Value = parameter.Value ?? DBNull.Value
});
}
string procWithParameter = string.Format("exec {0} {1}", procName, string.Join(", ", inputParameter.Select(p => p.ParameterName)));
var result = _dbContext.Database.SqlQueryRaw<T> (procWithParameter, inputParameter.ToArray());
return result.ToList();
//return result.ToList();
}
catch (System.Exception ex)
{
throw ex;
}
}
答案1
得分: 1
在EF中,您需要为每个查询结果定义一个类型。它可以是一个无键实体,或者使用EF Core 8中的任意类型,但您不能只运行任意SQL查询并使用结果。
在EF中,当实现查询结果时,变更跟踪不会追踪您是否加载了实体的单个属性,这会增加性能和复杂性成本,但带来的好处很少。
如果没有与结果匹配的实体,请使用Dapper或ADO.NET。
英文:
In EF you need to define an Type for each query result. It can be a Keyless Entity, or with EF Core 8 an arbitrary type, but you can't just run an arbitrary SQL query and use the results.
And when materializing query results EF change tracking doesn't track whether you've loaded individual properties of an Entity, which would add a perf and complexity cost for little benefit.
Use Dapper or ADO.NET if you don't have an Entity that matches the result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论