分享一个灰常好的 dapper 扩展插件: Rainbow

  dapper 是一个效率非常高的orm  框架 ,效率要远远大于 我们大微软的EF .    它只有一个类文件,非常之小。(在 EF 5.0 后 微软已经做了 改进)

   ps; 由于之前我也没测试过,只是看过官方之前的数据,还是实践出真知 。 在这里谢谢 深蓝医生 的指正。不过它还是一个非常优秀的微型orm框架。

   1,首先下载dapper  这里下载   .

   2,下载插件  Rainbow

    在Package Manager Console  中输入

1
PM> Install-Package Dapper.Rainbow

  

  准备工作 完成  下面是 demo 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
  
using Dapper;
  
// to have a play, install Dapper.Rainbow from nuget
  
namespace TestDapper
{
    class Program
    {
        // no decorations, base class, attributes, etc
        class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public DateTime? LastPurchase { get; set; }
        }
  
        // container with all the tables
        class MyDatabase : Database<MyDatabase>
        {
            public Table<Product> Products { get; set; }
        }
  
        static void Main(string[] args)
        {
            var cnn = new SqlConnection("Data Source=.;Initial Catalog=tempdb;Integrated Security=True");
            cnn.Open();
  
            var db = MyDatabase.Init(cnn, commandTimeout: 2);
  
            try
            {
                db.Execute("waitfor delay '00:00:03'");
            }
            catch (Exception)
            {
                Console.WriteLine("yeah ... it timed out");
            }
  
  
            db.Execute("if object_id('Products') is not null drop table Products");
            db.Execute(@"create table Products (
                    Id int identity(1,1) primary key,
                    Name varchar(20),
                    Description varchar(max),
                    LastPurchase datetime)");
  
            int? productId = db.Products.Insert(new {Name="Hello", Description="Nothing" });
            var product = db.Products.Get((int)productId);
  
            product.Description = "untracked change";
  
            // snapshotter tracks which fields change on the object
            var s = Snapshotter.Start(product);
            product.LastPurchase = DateTime.UtcNow;
            product.Name += " World";
             
            // run: update Products set LastPurchase = @utcNow, Name = @name where Id = @id
            // note, this does not touch untracked columns
            db.Products.Update(product.Id, s.Diff());
  
            // reload
            product = db.Products.Get(product.Id);
             
  
            Console.WriteLine("id: {0} name: {1} desc: {2} last {3}", product.Id, product.Name, product.Description, product.LastPurchase);
            // id: 1 name: Hello World desc: Nothing last 12/01/2012 5:49:34 AM
  
            Console.WriteLine("deleted: {0}", db.Products.Delete(product.Id));
            // deleted: True
  
  
            Console.ReadKey();
        }
    }
}

  

 

上面 这个MyDatabase ,实现了和 EF同样的机制 ,有了这个相当于EF 上下文的东西,就方便很多了, 拿到这个上下文后,就能直接操作所有的表了 ,方便了统一管理  ,用着很爽呀。。  .

 

  

posted @   ◇゛   仅此而已  阅读(8348)  评论(19编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示