Open Closed

How to query PostgreSQL Database Views in AppService #1775


User avatar
0
lalitChougule created
  • ABP Framework version: v4.3.1
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no / yes
  • Exception message and stack trace: N.A
  • Steps to reproduce the issue:" N.A

Hi,

I am using PostgreSQL and I have created few views in my database. How do I query those views in my AppService ?

  1. Should I create Entity for view ? My view does not contain Id.
  2. I am using custom repository https://docs.abp.io/en/abp/4.3/Repositories#custom-repository-example, How do I make custom repository for view
  3. How do I declare property in DbContext ? DbSet<ViewName>/DbQuery<ViewName>

Can you please provide step by step Implementation guide? I will be very helpful.

Thanks


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    https://github.com/abpframework/abp-samples/tree/master/StoredProcedureDemo

  • User Avatar
    0
    lalitChougule created

    Hi maliming,

    public async Task<List<string>> GetAdminUsernames(CancellationToken cancellationToken = default)
            {
                await EnsureConnectionOpenAsync(cancellationToken);
    
                using (var command = CreateCommand("SELECT * FROM dbo.UserAdminView", CommandType.Text))
                {
                    using (var dataReader = await command.ExecuteReaderAsync(cancellationToken))
                    {
                        var result = new List<string>();
    
                        while (await dataReader.ReadAsync(cancellationToken))
                        {
                            result.Add(dataReader["UserName"].ToString());
                        }
                        return result;
                    }
                }
            }
    

    Is this the only way to query view ? Can't we access it like normal query i.e.

    _someViewRepository.Where(x=> x.SomeValue == SomeValue).GroupBy(..)...
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try to use ToView.

    https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations#mapping-to-database-objects

  • 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 10, 2024, 06:38