Is there any way that could help me select specific data from table in Microsoft.AspNetCore.Datasync.EFCore

195 Views Asked by At

Am learning about data sync from API to WPF app. Got a demo from https://github.com/Azure/azure-mobile-apps/tree/main/samples. But I got into a problem that all the data inside the tables are collected on the call but I need to select specific data using Id. Tried a query etc all came to nothing. Please guide me Thank you

PatientsController.cs

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

    }
}

AppDbContext.cs

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }
    public DbSet<Patients> Patients => Set<Patients>();
}
2

There are 2 best solutions below

3
Yiyi You On

Try to use the following code:

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }
    public DbSet<Patients> Patients {get;set;}
}

controller:

[Route("tables/Patients")]
public class PatientsController : TableController<Patients>
{
    private readonly AppDbContext _context;
    public PatientsController(AppDbContext context)
        : base(new EntityTableRepository<Patients>(context))
    {
        _context=context;
    }
    public async Task<IActionResult> Index(int id){
         var Patients=_context.Patients.FindAsync(id);
         return View(Patients);
    }
}
0
Adrian Hall On

If you just need to get a record by Id, you use the URL https://mysite/tables/myTable/id - no search required - it will go directly to the entity you want.

If you need to limit what a user can see, you will need to implement an access control provider (which is an implementation of IAccessControlProvider). This provides three methods to limit what a user can see and do model updates for ensuring the write operations store the right thing. You can read more here: https://learn.microsoft.com/en-us/azure/developer/mobile-apps/azure-mobile-apps/howto/server/dotnet-core#configure-access-permissions

If it's something else, you'll need to be a little more specific. I hang out in the issues and discussions of the azure/azure-mobile-apps repo, so feel free to file a question there.