在谷歌上搜索了一段时间后,我了解到还不支持。如果上下文中没有模型,则无法使用实体框架检索数据,请指出:https://docs.microsoft.com/en-us/ef/core/querying/raw-sql和https :// github.com/aspnet/EntityFrameworkCore/issues/1862
我解决了旧方法:
var jsonResult = new System.Text.StringBuilder();
/*"using" would be bad, we should leave the connection open*/
var connection = _db.Database.GetDbConnection() as SqlConnection;
{
await connection.OpenAsync();
using (SqlCommand cmd = new SqlCommand(
"Mine",
connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@customerid", SqlDbType.NVarChar).Value = _user.guid;
using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
{
if (!reader.HasRows)
{
jsonResult.Append("[]");
}
else
{
while (reader.Read())
{
jsonResult.Append(reader.GetValue(0).ToString());
}
}
}
}
}
var raw = JArray.Parse(jsonResult.ToString());
var ret = raw.ToObject<List<SiteData>>();
我怀疑是否最好明确关闭连接。