2012-04-19 28 views
9

थोड़ा और अधिक उन्नत मानचित्रण फिर मेरे previous question :)डैप्पर मध्यवर्ती मानचित्रण

टेबल्स में:

create table [Primary] (
    Id int not null, 
    CustomerId int not null, 
    CustomerName varchar(60) not null, 
    Date datetime default getdate(), 
    constraint PK_Primary primary key (Id) 
) 

create table Secondary(
    PrimaryId int not null, 
    Id int not null, 
    Date datetime default getdate(), 
    constraint PK_Secondary primary key (PrimaryId, Id), 
    constraint FK_Secondary_Primary foreign key (PrimaryId) references [Primary] (Id) 
) 

create table Tertiary(
    PrimaryId int not null, 
    SecondaryId int not null, 
    Id int not null, 
    Date datetime default getdate(), 
    constraint PK_Tertiary primary key (PrimaryId, SecondaryId, Id), 
    constraint FK_Tertiary_Secondary foreign key (PrimaryId, SecondaryId) references Secondary (PrimaryId, Id) 
) 

क्लास:

public class Primary 
{ 
    public int Id { get; set; } 
    public Customer Customer { get; set; } 
    public DateTime Date { get; set; } 
    public List<Secondary> Secondaries { get; set; } 
} 

public class Secondary 
{ 
    public int Id { get; set; } 
    public DateTime Date { get; set; } 
    public List<Tertiary> Tertiarys { get; set; } 
} 

public class Tertiary 
{ 
    public int Id { get; set; } 
    public DateTime Date { get; set; } 
} 

public class Customer 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

यह एक का चयन का उपयोग करने के लिए उन्हें सभी को भरने के लिए संभव है? कुछ इस तरह:

const string sqlStatement = @" 
    select 
     p.Id, p.CustomerId, p.CustomerName, p.Date, 
     s.Id, s.Date, 
     t.Id, t.Date 
    from 
     [Primary] p left join Secondary s on (p.Id = s.PrimaryId) 
     left join Tertiary t on (s.PrimaryId = t.PrimaryId and s.Id = t.SecondaryId) 
    order by 
     p.Id, s.Id, t.Id 
"; 

और फिर:

IEnumerable<Primary> primaries = connection.Query<Primary, Customer, Secondary, Tertiary, Primary>(
    sqlStatement, 
    ... here comes dragons ... 
    ); 

Edit1 - मैं इसे दो नेस्टेड छोरों के साथ कर सकता (foreach द्वितीयक -> foreach tertiaries) और प्रत्येक आइटम के लिए एक प्रश्न करते हैं, लेकिन सिर्फ आश्चर्य है अगर यह एकल डेटाबेस कॉल के साथ किया जा सकता है।

संपादित 2 - शायद क्वेरी बहुउद्देश्यीय विधि यहां उचित होगी, लेकिन अगर मैं सही ढंग से समझता हूं तो मुझे कई चुनिंदा वक्तव्यों की आवश्यकता होगी। मेरे वास्तविक जीवन उदाहरण में चयन में 20 से अधिक स्थितियां हैं (जहां क्लॉज में), जहां खोज पैरामीटर शून्य हो सकता है, इसलिए मैं उन सभी प्रश्नों को दोहराना नहीं चाहूंगा जहां सभी प्रश्नों में बयान ...

उत्तर

4

डैप्पर मल्टी मानचित्रण, का समर्थन करता है के लिए दस्तावेज़ देखें विकल्प, एकाधिक ऑब्जेक्ट्स में रिकॉर्ड-सेट को विभाजित करने के लिए।

आप उपरोक्त उदाहरण के लिए कक्षा संरचना देखने के लिए मेरा प्रश्न भी देख सकते हैं: Dapper Multi-mapping Issue

1

जैसा लगता है सभी ओआरएम आपके पास कई प्रश्न होंगे। आप केवल अपना खुद का समाधान बना सकते हैं, शायद डैपर या पेटापोको पर आधारित। उदाहरण के लिए, एक एसक्यूएल बैच में सभी प्रश्नों गठबंधन:

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

तो फिर तुम DataReader.NextResult का उपयोग कर()

फिर, वस्तुओं संरचना पूरा करने के लिए स्मृति में डेटा को संयोजित करने की जरूरत द्वारा nex के लिए एक recordset से नेविगेट कर सकते हैं ।

1

SQLCommand बनाने के बारे में और फिर SQLParameter ऑब्जेक्ट्स का एक समूह बनाने के बारे में क्या। आदर्श रूप से एक संग्रहित proc के साथ लेकिन यह नहीं होना चाहिए।

उन आउटपुट पैरामीटर में से प्रत्येक को तब आपकी कक्षाओं में मैप किया जा सकता है।

This other post स्टैक पर कुछ कोड है जो प्रासंगिक हो सकता है। मैं वर्तमान पर काम कर रहा हूँ http://code.google.com/p/dapper-dot-net/

यहाँ परियोजनाओं में से एक से उदाहरणों में से एक है:

 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(); 

चाल splitOn उपयोग करने के लिए है

संबंधित मुद्दे