[ApiController]
public class MyController : ControllerBase
{
private readonly DataContext_context;
public MyController(DataContextcontext)
{
_context = context;
}
[HttpGet("{tableName}")]
public IActionResult GetList(string tableName, string? filter=null)
{
var entityType = _context.Model.FindEntityType(tableName);
if (entityType == null)
{
return NotFound();
}
var query = _context.Set(entityType.ClrType).AsQueryable();
if (!string.IsNullOrEmpty(filter))
{
// Apply filter to the query
query = query.Where($"{entityType.FindPrimaryKey().Properties.First().Name} = {filter}");
}
// Map to base model
var result = query.Select(entity =>
entityType.ClrType.Name switch
{
nameof(TableA) => new BaseModel { Id = entity.IdA, Name = entity.NameA },
nameof(TableB) => new BaseModel { Id = entity.IdB, Name = entity.NameB },
_ => throw new NotSupportedException($"Table {tableName} is not supported")
});
return Ok(result.ToList());
}
}
1条答案
按热度按时间jm2pwxwz1#
如果你能提供更多的细节,那就太好了。不过,下面的内容可能是一个好的开始: