使用 .Net 5 EF 从 abp.io 中的 CrudAppService 检索子实体

问题描述

我正在使用来自 abp.io 的最新版本的 ABP,并且有两个具有多对多关系的实体。它们是:

bids = [{'don': 200},{'alex': 400},{'peter': 550}]

bidder = None
values = []

for bid in bids:
    for k,v in bid.items():
        values.append(v)

bidder = max(values)
print(bidder)

public class GroupDto : AuditedEntityDto<Guid>
{
    public GroupDto()
    {
        this.Students = new HashSet<Students.StudentDto>();
    }

    public string Name { get; set; }
    public bool IsActive { get; set; } 
    public virtual ICollection<Students.StudentDto> Students { get; set; }
}

我设置了以下测试来检查我是否正在检索相关实体,不幸的是 public class StudentDto : AuditedEntityDto<Guid> { public StudentDto() { this.Groups = new HashSet<Groups.GroupDto>(); } public string Name { get; set; } public bool IsActive { get; set; } public virtual ICollection<Groups.GroupDto> Groups { get; set; } } 属性始终为空。

Students

对于 public async Task Should_Get_List_Of_Groups() { //Act var result = await _groupAppService.GetListAsync( new PagedAndSortedResultRequestDto() ); //Assert result.TotalCount.ShouldBeGreaterThan(0); result.Items.ShouldContain(g => g.Name == "13Ck" && g.Students.Any(s => s.Name == "Michael Studentman")); } List 的等效测试也是如此,Student 属性始终为空。

我找到了 abp.io 的一个相关答案(与 ABP 不同,它是一个更新/不同的框架)https://stackoverflow.com/a/62913782/7801941 但不幸的是,当我添加一个与 Groups 等效的内容时,我得到了错误 -

CS1061 'IRepository' 不包含定义 'Include' 和没有可访问的扩展方法 'Include' 接受 可以找到“IRepository”类型的第一个参数 (您是否缺少 using 指令或程序集引用?)

此代码如下,错误是在以 StudentAppService 开头的行上抛出的

.Include

实现这个接口

public class StudentAppService :
    CrudAppService<
        Student,//The Student entity
        StudentDto,//Used to show students
        Guid,//Primary key of the student entity
        PagedAndSortedResultRequestDto,//Used for paging/sorting
        CreateUpdateStudentDto>,//Used to create/update a student
    IStudentAppService //implement the IStudentAppService
{
    private readonly IRepository<Students.Student,Guid> _studentRepository;

    public StudentAppService(IRepository<Student,Guid> repository)
        : base(repository)
    {
        _studentRepository = repository;
    }

    protected override IQueryable<Student> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
    {
        return _studentRepository
             .Include(s => s.Groups);
    }
}

谁能说明我应该如何使用 public interface IStudentAppService : ICrudAppService< // Defines CRUD methods StudentDto,// Used to show students Guid,// Primary key of the student entity PagedAndSortedResultRequestDto,// Used for paging/sorting CreateUpdateStudentDto> // Used to create/update a student { // } 访问相关实体?

编辑:感谢那些做出回应的人。澄清一下,我正在寻找有关如何使用 AppService 而不是存储库访问具有多对多关系的实体的解决方案/解释。

为了帮助解决这个问题,我上传了我的整个源代码的 zip 文件,以及我为使其工作而尝试的许多更改,here

解决方法

您可以延迟加载、急切加载或为子集合的实体配置默认行为。

默认配置:

Configure<AbpEntityOptions>(options =>
{
    options.Entity<Student>(studentOptions =>
    {
        studentOptions.DefaultWithDetailsFunc = query => query.Include(o => o.Groups);
    });
});

急切负荷:

//Get a IQueryable<T> by including sub collections
var queryable = await _studentRepository.WithDetailsAsync(x => x.Groups);
        
//Apply additional LINQ extension methods
var query = queryable.Where(x => x.Id == id);
        
//Execute the query and get the result
var student = await AsyncExecuter.FirstOrDefaultAsync(query);

或延迟加载:

var student = await _studentRepository.GetAsync(id,includeDetails: false);
//student.Groups is empty on this stage

await _studentRepository.EnsureCollectionLoadedAsync(student,x => x.Groups);
//student.Groups is filled now

您可以查看docs了解更多信息。

编辑: 您可能忘记添加默认存储库,例如:

services.AddAbpDbContext<MyDbContext>(options =>
{
    options.AddDefaultRepositories();
});

虽然我想建议您使用像

这样的自定义存储库
IStudentRepository:IRepository<Student,Guid>

这样您就可以更好地扩展存储库。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...