我正在使用ASP.NET Core,并且想使用EntityFramework从数据库获取数据。 我使用下一个“ appsettings.json”
{
"ConnectionStrings": {
"MeteodataContex": "Server=77.222.146.91;Port=2800;Database=forecast;Uid=vntu_root;Pwd=politehluchevseh;"
}
}
和连接数据库的下一个类
namespace WebSolaris.Models
{
public class MeteodataContex : DbContext
{ public MeteodataContex() : base("MeteodataContex")
{
}
public DbSet<t_meteodata> meteodatas { get; set; }
}
}
还有获取数据的类
namespace WebSolaris.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SomeMeteoDataController : ControllerBase
{
public IActionResult Get(int stationId, DateTime from, DateTime to)
{
from = from.ToUniversalTime();
to = to.ToUniversalTime();
var met = new MeteoViewModel();
List<t_meteodata> responsemeteo;
using (var sd = new MeteodataContex())
{
{
responsemeteo = sd.meteodatas.Where(x => from <= x.DateTimeCr && x.DateTimeCr <= to)
.OrderBy(x => x.DateTimeCr)
.ToList();
met.SolarRadiation = responsemeteo.ToDictionary(x => x.DateTimeCr, z => z.SolarRadiation);
met.WindSpeed = responsemeteo.ToDictionary(x => x.DateTimeCr, z => z.WindSpeed);
met.Wetness = responsemeteo.ToDictionary(x => x.DateTimeCr, z => z.Wetness);
met.TemperatureC = responsemeteo.ToDictionary(x => x.DateTimeCr, z => z.TemperatureC);
}
return Ok(met);
}
}
}
}