分享一個灰常好的 dapper 擴展插件: Rainbow

  dapper 是一個效率很是高的orm  框架 ,效率要遠遠大於 咱們大微軟的EF .    它只有一個類文件,很是之小。(在 EF 5.0 後 微軟已經作了 改進)app

   ps; 因爲以前我也沒測試過,只是看過官方以前的數據,仍是實踐出真知 。 在這裏謝謝 深藍醫生 的指正。不過它仍是一個很是優秀的微型orm框架。框架

   1,首先下載dapper  這裏下載   .

   2,下載插件  Rainbow

    在Package Manager Console  中輸入ide

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 上下文的東西,就方便不少了, 拿到這個上下文後,就能直接操做全部的表了 ,方便了統一管理  ,用着很爽呀。。  .this

相關文章
相關標籤/搜索