dapper 扩展插件: Rainbow

dapper 是一个效率非常高的orm  框架 ,效率要远远大于 我们大微软的EF .    它只有一个类文件,非常之小。

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

2,下载插件  Rainbow

Package Manager Console  中输入

1
PM> Install-Package Dapper.Rainbow

  

准备工作 完成  下面是 demo 。

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 上下文的东西,就方便很多了, 拿到这个上下文后,就能直接操作所有的表了 ,方便了统一管理  ,用着很爽呀。。  .

 
 
分类: ORM
标签: micro-ormdapper

最新文章

  1. Code First开发系列之数据库迁移
  2. os and shutil
  3. QT 网络编程三(TCP版)
  4. PHP使用curl替代file_get_contents
  5. Spring RabbitMq
  6. Centos6 使用yum安装 mysql 5.7
  7. wamp2.5 不能运行在win2003的解决方法
  8. Linux 怎么重命名多个文件
  9. linux之稀疏文件
  10. HDU 3336 扩展kmp
  11. Java多线程之线程池
  12. MySQL 存储过程删除大表
  13. js 中的流程控制—while和do while
  14. Nagios的客户端的安装
  15. 使用HTML5 Canvas做些什么
  16. android获取ip和本机的物理地址
  17. 使用composer安装laravel
  18. Python javascript操作DOM
  19. 主流的Nosql数据库的对比
  20. MySQL--查看内存信息

热门文章

  1. .NET 中易混淆的概念(Delegate vs Event)
  2. c++双缓冲技术,以避免闪烁绘图
  3. Samza/KafkaAnalysizing
  4. POJ1719- Shooting Contest(二分图最大匹配)
  5. 深入理解PHP中赋值与引用
  6. Equals 和==
  7. Vim实用小技巧
  8. pyqt学习总结
  9. 欧几里德欧几里德原理和扩展的原则,(Euclidean Theory and Extended Euclidean Theory)学习笔记
  10. CRUD功能的JqGrid表格