如何制作Abp.Dapper多表查询?

问题描述 投票:-1回答:1

我有一个实体模型Person -> Phones(collection)和以下代码

[Table("PbPersons")]
public class Person: FullAuditedEntity, IMustHaveTenant
{
    public const int MaxNameLength = 32;
    public const int MaxSurnameLength = 32;
    public const int MaxEmailAddressLength = 255;

    public virtual int TenantId { get; set; }

    [Required]
    [MaxLength(MaxNameLength)]
    public virtual string Name { get; set; }

    [Required]
    [MaxLength(MaxSurnameLength)]
    public virtual string Surname { get; set; }

    [MaxLength(MaxEmailAddressLength)]
    public virtual string EmailAddress { get; set; }

    public virtual ICollection<Phone> Phones { get; set; }

}

手机

[Table("PbPhones")]
public class Phone : CreationAuditedEntity<long>
{
    public const int MaxNumberLength = 16;

    [ForeignKey("PersonId")]
    public virtual Person Person { get; set; }
    public virtual int PersonId { get; set; }

    [Required]
    public virtual PhoneType Type { get; set; }

    [Required]
    [MaxLength(MaxNumberLength)]
    public virtual string Number { get; set; }

}

与Abp.EF的旧应用程序层

此代码返回包括电话在内的人员列表

using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Dapper.Repositories;
using Abp.Domain.Repositories;
using Abp.Extensions;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;

public class PersonAppService : WebAppServiceBase, IPersonAppService
{
private readonly IRepository<Person> _personRepository;
private readonly IRepository<Phone, long> _phoneRepository;

public PersonAppService(IRepository<Person> personRepository, 
IRepository<Phone, long> phoneRepository)        
{
    _personRepository = personRepository;
    _phoneRepository = phoneRepository;
}

 public ListResultDto<PersonListDto> GetPeople(GetPeopleInput input)
{

            var persons = _personRepository
            .GetAll()
            .Include(p => p.Phones)
            .WhereIf(
                !input.Filter.IsNullOrEmpty(),
                p => p.Name.Contains(input.Filter) ||
                        p.Surname.Contains(input.Filter) ||
                        p.EmailAddress.Contains(input.Filter)
            )
            .OrderBy(p => p.Name)
            .ThenBy(p => p.Surname)
            .ToList();

            return new ListResultDto<PersonListDto>(Mapper.Map<List<Person>, List<PersonListDto>>(persons));
}

}

Abp.Dapper的新应用程序层

但是当我使用Abp.Dapper时,GetAll方法不允许包含任何方法,如Include / WhereIf,我知道这是因为它实现了IEnumerable接口。我需要它还返回每个人的电话列表。我该怎么办?,我尝试使用linq(包括,它不可用)但没有成功。

 using Abp.Application.Services.Dto;
 using Abp.Authorization;
 using Abp.Dapper.Repositories;
 using Abp.Domain.Repositories;
 using Abp.Extensions;
 using AutoMapper;
 using Microsoft.EntityFrameworkCore;
 using System.Collections.Generic;
 using System.Linq;

 public class PersonAppService : WebAppServiceBase, IPersonAppService
 {
private readonly IDapperRepository<Person> _personRepository;
private readonly IRepository<Phone, long> _phoneRepository;

public PersonAppService(IDapperRepository<Person> personRepository, 
IRepository<Phone, long> phoneRepository)        
{
    _personRepository = personRepository;
    _phoneRepository = phoneRepository;
}

 public ListResultDto<PersonListDto> GetPeople(GetPeopleInput input)
{

            var persons = _personRepository.GetAll()              
            .OrderBy(p => p.Name)
            .ThenBy(p => p.Surname)
            .ToList();

            return new ListResultDto<PersonListDto>(Mapper.Map<List<Person>, List<PersonListDto>>(persons));
}

}

dapper aspnetboilerplate
1个回答
0
投票

请参阅获取导航数据的测试代码。

 public async Task Should_Include_Navigation_Properties_If_Requested()
        {
            using (var uow = _uowManager.Begin())
            {
                var post = await _postRepository.GetAllIncluding(p => p.Blog).FirstAsync();

                post.Blog.ShouldNotBeNull();
                post.Blog.Name.ShouldBe("test-blog-1");

                await uow.CompleteAsync();
            }
}

https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/test/Abp.EntityFrameworkCore.Tests/Tests/Repository_Tests.cs#L80

© www.soinside.com 2019 - 2024. All rights reserved.