c# - 在ABP中从CrudAppService检索子实体

标签 c# asp.net-core parent-child entity-framework-core aspnetboilerplate

现成的GetAllGetCrudAppService方法不包括子实体。

是否可以修改其行为?

更新资料

如果包含的实体具有到父级的导航属性,则GetAllIncluding会出现问题。它属于一种循环依赖关系。是否有任何Attribute或技巧可以将导航属性从序列中排除? [NonSerialized]属性似乎不适用于导航属性。

PostAppService:

public class PostAppService : CrudAppService<Post, PostDto>, IPostAppService
{
    IRepository<Post> _repository = null;

    public PostAppService(IRepository<Post> repository) : base(repository)
    {
        _repository = repository;
    }

    protected override IQueryable<Post> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
    {
        return _repository.GetAllIncluding(p => p.Items);
    }
}


PostDto:

[AutoMap(typeof(Post))]
public class PostDto : EntityDto
{
    public ICollection<Item> Items { get; set; }
}


职位实体:

[Table("AbpPosts")]
public class Post : FullAuditedEntity<int,User>
{
    public virtual ICollection<Item> Items { get; set; }
}


项目实体:

[Table("AbpItems")]
public class Item : Entity
{
    [ForeignKey("PostId")]
    public Post Post { get; set; }
    public int PostId { get; set; }
}

最佳答案

您必须使用预先加载。

在您的AppService中覆盖CreateFilteredQueryGetEntityById

public class MyAppService : CrudAppService<ParentEntity, ParentEntityDto>, IMyAppService
{
    public MyAppService(IRepository<ParentEntity> repository)
        : base(repository)
    {
    }

    protected override IQueryable<ParentEntity> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
    {
        return Repository.GetAllIncluding(p => p.ChildEntity);
    }

    protected override ParentEntity GetEntityById(int id)
    {
        var entity = Repository.GetAllIncluding(p => p.ChildEntity).FirstOrDefault(p => p.Id == id);
        if (entity == null)
        {
            throw new EntityNotFoundException(typeof(ParentEntity), id);
        }

        return entity;
    }
}


覆盖这些方法的好处是您可以继续免费获得permission checkingcountingsortingpagingmapping

更新资料


  如果包含的实体具有到父级的导航属性,则GetAllIncluding会出现问题。它属于一种循环依赖关系。是否有任何Attribute或技巧可以将导航属性从序列中排除?


ItemDto中返回PostDto(无导航属性)。

关于c# - 在ABP中从CrudAppService检索子实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48403273/

相关文章:

html - CSS 未相对于父级应用

c# - 父控件ScrollViewer滚动而不是子控件ScrollViewer

c# - 我怎样才能找到以前表格的表格名称?

c# - 如何以编程方式在 C# 中使用 "using"关键字?

javascript - MVC 中多选的 Select2 设置值

.net - .net core 3.1 web api 中的多个 get 方法

c# - 部署 ASP.NET Core Docker 项目 - 收到 405 错误(在我的 IIS 本地,Web 请求有效)。如何解决?

c# - 允许用户编辑/添加/移动图像中的项目

c# - ASP.NET Core - 使用 [FromServices] 属性可能会带来哪些挑战或问题?

jQuery 在单击时从父级中删除类