它很简单:我没有这个结果的模型/视图模型。我不想拥有它。我只想调用我的存储过程并创建一个动态类型。像这样的东西:
dynamic ceps = (from cep in context.Database.SqlQuery<dynamic>("spLogradouro @pCEP, @pUF, @pLOCALIDADE, @pTIPOLOGRADOURO, @pLOGRADOURO", new SqlParameter("pCEP", filter), new SqlParameter("pUF", ""), new SqlParameter("pLOCALIDADE", ""), new SqlParameter("pTIPOLOGRADOURO", ""), new SqlParameter("pLOGRADOURO", ""))
select new
{
property_one = name_of_my_column_of_this_stored_procedure_return
}).Single();
不可能那么难。
编辑
安装我正在寻找的东西,但要实现我的目标:
CepContext context = new CepContext();
JsonResult result = Json(new { Street = "", District = "", City = "", UF = "" });
using (var cmd = context.Database.Connection.CreateCommand())
{
cmd.CommandText = "spLogradouro @pCEP, @pUF, @pLOCALIDADE, @pTIPOLOGRADOURO, @pLOGRADOURO";
cmd.Parameters.Add(new SqlParameter("pCEP", filter));
cmd.Parameters.Add(new SqlParameter("pUF", ""));
cmd.Parameters.Add(new SqlParameter("pLOCALIDADE", ""));
cmd.Parameters.Add(new SqlParameter("pTIPOLOGRADOURO", ""));
cmd.Parameters.Add(new SqlParameter("pLOGRADOURO", ""));
cmd.Connection.Open();
var reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
result = Json(new
{
Street = reader.GetString(reader.GetOrdinal("log_logradouro")),
District = reader.GetString(reader.GetOrdinal("log_bairro")),
City = reader.GetString(reader.GetOrdinal("log_localidade")),
UF = reader.GetString(reader.GetOrdinal("log_uf"))
});
}
cmd.Connection.Close();
}