嗨,我想直接从控制器中添加带有模型的数据,而无需执行其他任何操作。
void m(Something a, SomethingElse b, YetAnotherThing c, ANewThing d) {
// ...
}
// or
void m(Something a, SomethingElse b) {
// ...
}
// or
void m(ANewThing d) {
// ...
}
...我希望能够直接从数据库中获取数据:
namespace WebApplication3.Controllers
{
public class ApiController : Controller
{
readonly HumanDBContext _humanContext;
-修复程序在这里:
[HttpGet]
public JsonResult Godaddy()
{
和
public class ApiController : Controller
{ private readonly HumanDBContext _db;
答案 0 :(得分:0)
您可以执行以下操作:
public jsonResult Get()
{
DbCommand cmd = _humanContext.DbContext.Database.GetDbConnection().CreateCommand()
cmd.CommandText = "yourspName";
cmd.CommandType = CommandType.StoredProcedure;
using (IDataReader reader = await cmd.ExecuteReaderAsync())
{
List<Foo> items= reader.Select(r => r.GetYourPocoClassModel()).ToList();
string rItems = JsonConvert.SerializeObject(items, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return rItems;
}
}
和GetYourPocoClassModel用于对数据建模:
public Calss HelperExtension
{
public static Foo GetYourPocoClassModel(this IDataReader r)
{
return new Foo
{
Id=r["Id"] is DBNull ? 0 : Convert.ToDouble(r["Id"]),
}
}
}