Open Closed

How to get a list from a EF database inside a controller? #1733


User avatar
0
nparejod created
  • ABP Framework version: v4.4.0
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

Hello,

I am trying to implement Entity extention for abapusers as per: https://docs.abp.io/en/abp/4.4/Module-Entity-Extensions#module-entity-extensions

In Version 4.4.0, what would be the best approach to extract data from database and generate a list in json format to be used by the abp user form? I am trying to create a lookup field to pick customer ID.

An example implementation that returns a fixed list of departments (in real life, you get the list from a data source):

[Route("api/departments")]
public class DepartmentController : AbpController
{
    [HttpGet]
    public async Task<ListResultDto<DepartmentDto>> GetAsync()
    {
        return new ListResultDto<DepartmentDto>(
            new[]
            {
                new DepartmentDto
                {
                    Id = Guid.Parse("6267f0df-870f-4173-be44-d74b4b56d2bd"),
                    Name = "Human Resources"
                },
                new DepartmentDto
                {
                    Id = Guid.Parse("21c7b61f-330c-489e-8b8c-80e0a78a5cc5"),
                    Name = "Production"
                }
            }
        );
    }
}

The code above is hardcoded list of Departments, how can I get the same list of departments from a database?

Thanks,

Nestor


2 Answer(s)
  • User Avatar
    0
    yekalkan created
    Support Team Fullstack Developer

    Hi @nparejod,

    You can inject IRepository<Department, Guid> and use it to perform query on database.

    It would be better to create a domain service for this lookup list operation.

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 12, 2024, 07:15