You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Microsoft.AspNetCore.Datasync.EFCore中按ID筛选表中特定数据?

解决方案:按ID筛选Azure Mobile Apps接口数据

方法1:利用内置OData查询参数(无需修改后端)

Azure Mobile Apps的TableController默认支持OData查询语法,前端调用时直接在URL后追加筛选参数即可获取特定ID的患者数据:

GET /tables/Patients?$filter=Id eq '目标ID值'

示例:如果要获取ID为patient123的记录,请求URL为:

GET /tables/Patients?$filter=Id eq 'patient123'

方法2:添加自定义REST风格接口(推荐)

在PatientsController中新增一个根据ID获取单条记录的Action,符合RESTful规范:

[Route("tables/Patients")]
public class PatientsController : TableController<Patients>
{
    public PatientsController(AppDbContext context)
        : base(new EntityTableRepository<Patients>(context))
    {
    }

    // 根据ID获取单个患者
    [HttpGet("{id}")]
    public async Task<IActionResult> GetPatientById(string id)
    {
        var patient = await Repository.LookupAsync(id);
        if (patient == null)
        {
            return NotFound();
        }
        return Ok(patient);
    }
}

前端调用时直接请求:

GET /tables/Patients/目标ID值

示例:GET /tables/Patients/patient123

注意事项

  • 确保你的Patients实体类主键字段为Id(Azure Mobile Apps默认主键约定),如果主键名称不同,需要在实体字段上添加[TableColumn(TableColumnType.Id)]特性标记。
  • 两种方法可以同时存在,OData方式支持更复杂的筛选(如多条件组合),自定义Action更适合明确获取单条记录的场景。

内容的提问来源于stack exchange,提问作者Michael Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 01:20:19