现成的GetAll
的Get
和CrudAppService
方法不包括子实体。
是否可以修改其行为?
更新资料
如果包含的实体具有到父级的导航属性,则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中覆盖CreateFilteredQuery
和GetEntityById
:
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 checking,counting,sorting,paging和mapping。
更新资料
如果包含的实体具有到父级的导航属性,则
GetAllIncluding
会出现问题。它属于一种循环依赖关系。是否有任何Attribute
或技巧可以将导航属性从序列中排除?在
ItemDto
中返回PostDto
(无导航属性)。
关于c# - 在ABP中从CrudAppService检索子实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48403273/