Dapper intermediate mapping

后端 未结 3 870
盖世英雄少女心
盖世英雄少女心 2020-12-31 16:21

Slightly more advanced mapping then in my previous question :)

Tables:

create table [Primary] (
    Id int not null,
    CustomerId int not null,
            


        
相关标签:
3条回答
  • 2020-12-31 16:50

    Dapper supports Multi Mapping, for documentation see: http://code.google.com/p/dapper-dot-net/

    Here is one of the examples from one of the projects I'm currently working on:

            var accounts2 = DbConnection.Query<Account, Branch, Application, Account>(
                        "select Accounts.*, SplitAccount = '', Branches.*, SplitBranch = '', Applications.*" +
                        " from Accounts" +
                        "    join Branches" +
                        "       on Accounts.BranchId = Branches.BranchId" +
                        "    join Applications" +
                        "       on Accounts.ApplicationId = Applications.ApplicationId" +
                        " where Accounts.AccountId <> 0",
                        (account, branch, application) =>
                        {
                            account.Branch = branch;
                            account.Application = application;
                            return account;
                        }, splitOn: "SplitAccount, SplitBranch"
                        ).AsQueryable();
    

    The trick is to use the splitOn option, to divide record-set into multiple objects.

    You can also check my question to see class structure for the above example: Dapper Multi-mapping Issue

    0 讨论(0)
  • 2020-12-31 16:50

    What about creating a SQLCommand, and then a bunch of SQLParameter objects. Ideally with a stored proc but doesn't have to be.

    Each of those output parameters could then be mapped back to your classes.

    This other post on Stack has some code that could be relevant.

    0 讨论(0)
  • 2020-12-31 16:52

    Seems like in all ORMs you will have several queries. You only can create your own solution, maybe based on Dapper or Petapoco. For example, combine all queries in one SQL batch:

    select * from Primary where ...
    select * from Secondary where ...
    select * from Tertiary where ...
    

    Then you can navigate from one recordset to nex by using DataReader.NextResult()

    Then, need to combine data in memory to complete objects structure.

    0 讨论(0)
提交回复
热议问题