Base solution for your next web application
Open Closed

EntityFramework - how to "include" in context #1254


User avatar
0
ofir1234 created

Hi. In regular EntityFramework I can just write

Context.Blogs.include(b=>b.posts):

In order to automatically fill posts inside my Blog context. Without this, I m getting null in the posts field when reading the Blogs table using the GetAllList command. How can I do it in boilerplate ? Cause all I do is writing : IDbSet<...> Blogs And that's all I do in order to define the context in boilerplate.

Actually I want to do what's written in here : <a class="postlink" href="https://msdn.microsoft.com/en-us/data/jj574232.aspx">https://msdn.microsoft.com/en-us/data/jj574232.aspx</a>

Thanks


2 Answer(s)
  • User Avatar
    0
    abdllh created

    You don't have to include on application side If you do correct mapping between Dto and entities. Here is the example for your need:

    // In Core Project
    
    // Blog.cs
    public class Blog : Entity
    {
    	public const int MaxNameLength = 256;
    
    	public string Name { get; set; }
    	public virtual ICollection<Post> Posts { get; set; }
    }
    
    // Post.cs
    public class Post : Entity
    {
    	[ForeignKey("BlogId")]
    	public virtual Blog Blog { get; set; }
    	public int? BlogId { get; set; }
    	
    	public string Content { get; set; }
    }
    
    // In Application Project
    
    // BlogDto.cs
    [AutoMap(typeof(Blog))]
    public class BlogDto : EntityDto
    {
    	public string Name { get; set; }
    }
    
    // PostDto.cs
    [AutoMap(typeof(Post))]
    public class PostDto : EntityDto
    {
    	public string Content { get; set; }
    }
    
    
    // BlogWithPostsDto.cs
    public class BlogWithPostsDto : BlogDto
    {
    	public virtual IList<PostDto> Posts { get; set; }
    }
    
    // GetAllBlogsWithPostsInput.cs
    public class GetAllBlogsWithPostsInput : IInputDto
    {
    	[MaxLength(Blog.MaxNameLength)]
    	public string Filter { get; set; }
    }
    
    // IBlogAppService.cs
    public IBlogAppService : IApplicationAppService
    {
    	Task<ListResultOutput<BlogWithPostsDto>> GetAllBlogsWithPosts(GetAllBlogsWithPostsInput input);
    }
    
    // BlogAppService.cs
    public BlogAppService : MyApplicationAppServiceBase, IBlogAppService
    {
    	private readonly IRepository<Blog> _blogRepository;
    	
    	public BlogAppService(IRepository<Blog> blogRepository)
    	{
    		_blogRepository = blogRepository;
    	}
    
    	public async Task<ListResultOutput<BlogWithPostsDto>> GetAllBlogsWithPosts(GetAllBlogsWithPostsInput input)
    	{
    		var blogs = _blogRepository
    			.GetAll()
    			.WhereIf(!input.Filter.IsNullOrEmpty(), b => b.Name.Contains(input.Filter))
    			.ToList();
    			
    		return new ListResultOutput<BlogWithPostsDto>(blogs.MapTo<List<BlogWithPostsDto>>());
    	}
    }
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Include is defined in EntityFramework assembly. So, if you reference to EntityFramework from your project, then you can use it like that:

    repository.GetAll().Include(...).ToList();