question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able map joined entities into main model

See original GitHub issue

Linq2 DB not mapped child entities on custom dto entities

I not find in documentation how do it, but I want auto map from my sql query with join entities this child entities to list, but have only mapped main entity like in plain sql use.

Note: I not use assotions for it becouse in my case I have one generic field for many other entities.

    public class StoreVm
    {
        public StoreVm()
        {
            Documents = new List<DocumentVm>();
        }
        public long CardId { get; set; }
        public string Title { get; set; }
        public StoreType Type { get; set; }
        public long Id { get; set; }
        public DateTimeOffset CreatedOn { get; set; }
        public List<DocumentVm> Documents { get; set; }
    }
    
    public class DocumentVm
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
    }
    
    var sqlQuery = new StringBuilder($"SELECT \"Store\".\"CardFK\" AS \"CardId\", \"Store\".\"CreatedOn\", \"Store\".\"Title\", \"Store\".\"Type\", \"Store\".\"DeletedOn\", \"Store\".\"Id\",\r\n\t\"Document\".\"Id\", \"Document\".\"Name\", \"Document\".\"Path\" FROM public.\"Store\" JOIN public.\"Document\" ON \"Store\".\"Id\" = \"Document\".\"RecordId\" WHERE \"Document\".\"RecordType\" = 1 AND \"Store\".\"CardFK\" = 35");
    
    var resQuery = DbConnection.FromSql<StoreVm>(sqlQuery.ToString()).ToArray(); //not mapped documents vm
    
    

Environment details

Linq To DB version: 4.3.0

Database (with version): PostrgeSQL 14

Operating system: Windows

.NET (Core): 6

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Dec 27, 2022

Actually not planned because linq2db is more LINQ oriented, but for sure it can be implemented if we design useful API for that. Check how it works in Dapper https://www.learndapper.com/relationships - my thoughts: LINQ is better.

0reactions
Insanenotfromthisworldcommented, Dec 27, 2022

@sdanyliv So thanks will know. Does it planned on feature(collection properties mapping)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to map collection in onetomany mapping JPA
Look at your mapping. You are trying to map a FK in the element table. @OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "item_id", ...
Read more >
Advanced table mapping - EF Core
EF Core allows to map two or more entities to a single row. ... between the primary key of one entity type and...
Read more >
Chapter 2. Mapping Entities
If the property is of a single type, it is mapped as @Basic. Otherwise, if the type of the property is annotated as...
Read more >
Automatically Mapping DTO to Entity on Spring Boot APIs
Let's learn how ModelMapper can help us automate the mapping process of DTOs into entities on Spring Boot APIs.
Read more >
Hibernate Tips: How to map a bidirectional many-to- ...
Solution: You need to model the association on both entities if you want to be able to navigate it in both directions. Let's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found