[Original] duplicate wheels of high imitation EntityFramework, high imitation entityframework

Source: Internet
Author: User

[Original] duplicate wheels of high imitation EntityFramework, high imitation entityframework

  • Preface

In the previous article "[original] building a Dapper-based data access layer", Dapper often needs to manually write SQL statements in scenarios such as multi-table free association, group query, and anonymous query. Looking at the Red SQL string in the full screen of the Code, it was very big, so I came up with the idea of repeating the ORM wheel. This ORM draws the best effort from EF writing in API design and supports chained query (point tag), query expression, aggregate query, group sorting, batch insertion, batch update, batch deletion, and foreign key. At the entity binding level, Emit is used to dynamically build binding commands, and the performance is as close as native.

  • Performance

7000 records are read cyclically for 1000 times, and foreign keys with a relationship are loaded at a faster speed than EF.

  • Syntax

1. Single Table query

// Query expression var query = from a in context. getTable <Inte_CRM.Demo> () select a; var r1 = query. toList (); // mark query = context. getTable <Inte_CRM.Demo> (); r1 = query. toList (); // SQL => // SELECT // t0.[ DemoId] AS [DemoId], // t0.[ DemoCode] AS [DemoCode], // t0.[ DemoName] AS [DemoName], //... // t0.[ DemoLong] AS [DemoLong], // t0.[ DemoLong_Nullable] AS [DemoLong_Nullable] // FROM [Sys_Demo] t0

2. join query

// INNER JOINvar query = from a in context. getTable <Inte_CRM.CRM_SaleOrder> () join B in context. getTable <Inte_CRM.Client> () on. clientId equals B. clientId join c in context. getTable <Inte_CRM.CloudServer> () on B. cloudServerId equals c. cloudServerId where. clientId> 0 select a; var r1 = query. toList (); // mark query = context. getTable <Inte_CRM.CRM_SaleOrder> (). join (context. getTable <Inte_CRM.Client> (), a =>. clientId, B => B. clientId, (a, B) => new {Sale = a, Buyer = B }). join (context. getTable <Inte_CRM.CloudServer> (), B => B. buyer. cloudServerId, c => c. cloudServerId, (a, c) => new Inte_CRM.CRM_SaleOrder {}). where (a =>. clientId> 0); // r1 = query. toList (); // SQL => // SELECT // t0.[ OrderId] AS [OrderId], // t0.[ OrderNo] AS [OrderNo], // t0. [Remark] AS [Remark], // t0.[ ClientId] AS [ClientId] // FROM [CRM_SaleOrder] t0 // inner join [Bas_Client] t1 ON t0.[ ClientId] = t1. [ClientId] // inner join [sys_CloudServer] t2 ON t1. [CloudServerId] = t2. [CloudServerId] // WHERE t0.[ ClientId]> 0

3. Group Paging

// Query by PAGE after grouping = from a in context. getTable <Inte_CRM.Client> () where. clientName = "TAN" group a by new {. clientId,. clientName} into g where g. key. clientId> 0 orderby new {g. key. clientName, g. key. clientId} select new {Id = g. key. clientId, Name = g. min (a =>. clientId)}; query = query. skip (2 ). take (3); r1 = query. toList (); // SQL => // SELECT // t0. [Id], // t0.[ Name] // FROM (// SELECT // t0.[ ClientId] AS [Id], // MIN (t0.[ ClientId]) AS [Name], // t0.[ ClientName] AS [ClientName] // FROM [Bas_Client] t0 // WHERE t0.[ ClientName] = n'tan' // group by t0.[ ClientId], t0.[ ClientName] // Having t0.[ ClientId]> 0 //) t0 // order by t0.[ ClientName] // OFFSET 2 rows fetch next 3 ROWS ONLY

4. Batch insert

context.Insert<Inte_CRM.Thin>(collection);context.SubmitChanges();//SQL=> //INSERT INTO[Sys_Thin]//([ThinId],[ThinName])//VALUES//(2, N'002'),(3,N'003')

5. Navigation Properties

// Simpler Assignment Method // applicable scenario: only one or two fields of the foreign key table query = from a in context are displayed when the list is displayed. getTable <Inte_CRM.CRM_SaleOrder> () select new Inte_CRM.CRM_SaleOrder (a) {Client = new Inte_CRM.Client (. client) {CloudServer = new Inte_CRM.CloudServer {CloudServerId =. client. cloudServer. cloudServerId, CloudServerName =. client. cloudServer. cloudServerName }}, HeavyBuyer = new Inte_CRM.Client {ClientId =. client. clientId + 10, ClientName =. client. clientName + "_ heavy", CloudServer = new Inte_CRM.CloudServer {CloudServerId =. client. cloudServer. cloudServerId + 10, CloudServerName =. client. cloudServer. cloudServerName + "_ heavy" ,}}; r1 = query. toList (); // SQL => // SELECT // t0.[ OrderId] AS [OrderId], // t0.[ OrderNo] AS [OrderNo], // t0.[ Remark] AS [Remark], // t0.[ ClientId] AS [ClientId], // t1. [ClientId] AS [ClientId1], // t1. [ClientCode] AS [ClientCode], // t1. [ClientName] AS [ClientName], // t1. [State] AS [State], // t1. [ActiveDate] AS [ActiveDate], // t1. [CloudServerId] AS [CloudServerId], // t2. [CloudServerId] AS [CloudServerId1], // t2. [CloudServerName] AS [CloudServerName], // t1. [ClientId] + 10 AS [ClientId2], // t1. [ClientName] + N' _ heavy 'AS [ClientName1], // t2. [CloudServerId] + 10 AS [CloudServerId2], // t2. [CloudServerName] + N' _ heavy 'AS [CloudServerName1] // FROM [CRM_SaleOrder] t0 // left join [Bas_Client] t1 ON t0. [ClientId] = t1. [ClientId] // left join [Sys_CloudServer] t2 ON t1. [CloudServerId] = t2. [CloudServerId]

 

Other more examples in the source code demo has a detailed description, Source Code address: https://github.com/TANZAME/Inte.XFramework

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.