Dapper's ultra-lightweight open source based Lambda expansion, Dapper.Common

Dapper.Common

  Dapper.Common is based Lambda extension Dapper, following Linq grammar rules, quick, support Mysql, Sqlserver (currently only implements two data, other databases is also very easy to achieve), supports single-table, multi-table, custom functions and other functions.

  Open Source Address: https://github.com/1448376744/Dapper.Common

  Nuget:Install-Package Dapper.Common -Version 1.5.0

1.Mapper

 

  public class User
    {
        ///  <Summary> 
        /// name: database for mapping field names and field inconsistencies
         /// Key:
         ///      when the current implementation of the definition of Primary, Primary field is set to update the entity, the field is updated using the default condition
         /// isIdentity:
         ///      does not set any set value to true when Insert field
         /// isColumn:
         ///      identifies whether the field exists in the database, to extend without generating the User field in the sql
         ///  </ Summary> 
        [the Column (name: " ID " , Key: ColumnKey.Primary, isIdentity: to true , isColumn: to true )]
         public  int ? Id { GET ; SET ;}
        [Column(name:"nick_name")]
        public string NickName { get; set; }
        [Column(name: "create_time")]
        public DateTime? CreateTime { get; set; }
    }

 

2.Config

// execute once when the App start 
SessionFactory.AddDataSource ( new new the DataSource ()
{
    Name = "mysql",
    Source = () => new SqlConnection("connectionString"),
    SourceType = DataSourceType.SQLSERVER,
    UseProxy = to true // use the static proxy Session implemented, logging, time-consuming 
});
 // get database context 
the using ( var the session SessionFactory.GetSession = ( " mSQL " ))
{
    //linq to sql
}

 

 

3.Insert

var entity = new User()
{
    CreateTime=DateTime.Now,
    NickName="dapper",
};
// Most of the interface can set the condition has decided whether to support batch updates 
session.From <the User> () Insert (the Entity, condition:. 1 > 2 );
 // View Log 
var Loggers = session.Loggers;

2.Update

 // update all fields (where id = 2), supports batch 
 session.From <the User> () .Update (Entity);

 // Update Section Field 
 session.From <the User> ()
     .Set (A => a.NickName, " John Doe " , for condition Condition: to true ) // for condition Condition field is updated to true 
     .set (A => a.Balance, A => a.Balance + 100 ) // Balance increase in the original base 100 
     .Where (a => a.Id.In ( . 1 , 2 , . 3 )) // will be updated to record id 1,2,3 
     .Update ();

3.Delete

  //删除id>5||nick_name like '%da%'
  session.From<User>()
      .Where(a=>a.Id>5||a.NickName.Like("da"))
      .Delete();

4.Single

  // query all the fields 
  var user1 = session.From <the User> ()
      .Where(a=>a.Id==2)
      .Single();
 
  // query field portion 
  var user2 = session.From <the User> ()
     .Where(a => a.Id == 2)
     .Single(s=>new
     {
         s.Id,
         s.NickName
     });

5.Select

 //查询:where id in(1,2,3)
 var list = session.From<User>()
        .Where(a => a.Id.In("1,2,3".Split(',')))
        .Select();

 

6.Where

 

 // build dynamic query, condition: true performed, where a plurality of connections between and 
 var List = session.From <the User> ()
        .Where(a => a.Id.In(1, 2, 3), condition: true)
        .Where(a => a.NickName.Like("da"), condition: false)
        .Where(a => a.Id > 2 || (a.NickName.Like("da") && a.Balance > 50))
        .Where ( " the SELECT * from user_bill the WHERE user_bill.user_id = user.id " ) // can also be used as string concatenation tool 
        .Select ();

 

7.Function

 ///  <Summary> 
 /// custom function
  ///  </ Summary> 
 public  static  class MySqlFun
 {
     // Here the use of generics is not necessary, only the function name can exist in the database, in order to specify the generic return type of data 
     [Function] // Dapper.Common strict distinction between C # functions and database functions, be sure to use this feature logo database functions 
     public  static T COUNT <T> (T column)
     {
         return default(T);
     }
     [Function]
     public static T MAX<T>(T column)
     {
         return default(T);
     }
     [Function]
     public static T DISTINCT<T>(T column)
     {
         return default(T);
     }

8.GroupBy

 // build dynamic query, condition: true executed, and a plurality of connections between WHERE 
 var List = session.From <the Order> ()
     .GroupBy(a => a.UserId)
     .Having(a => MySqlFun.COUNT(MySqlFun.DISTINCT(a.UserId)) > 10)//count(distinct(user_id))>10
     .Select(s => new
     {
         s.UserId,
         OrderCount = MySqlFun.COUNT ( 1L ), // here should return int Long, 
         MaxFee = MySqlFun.MAX (s.TotalFee)
     });

9.Join

 var list = session.From<Order, User>()
     .Join((a, b) => a.UserId == b.Id, JoinType.Inner)
     .GroupBy((a, b) => a.UserId)
     .Having((a, b) => MySqlFun.COUNT(MySqlFun.DISTINCT(a.UserId)) > 10)//count(distinct(user_id))>10
     .Select((a, b) => new
     {
         a.UserId,
         b.NickName,
         OrderCount = MySqlFun.COUNT ( 1L ), // here should return int Long, 
         MaxFee = MySqlFun.MAX (a.TotalFee)
     });

10.SubQuery

var list = session.From<Order>()
    .GroupBy(a  => a.UserId)
    .Having(a => MySqlFun.COUNT(MySqlFun.DISTINCT(a.UserId)) > 10)
    .Select(a => new
    {
        a.UserId,
        UserName = Convert.ToString ( " the SELECT NICK_NAME as a user.id the WHERE = order.user_id from the User " ), // if the subquery returns int: Convert.ToInt32 (sq) 
        OrderCount = MySqlFun.COUNT ( 1L ), // It should return int Long, 
        MaxFee = MySqlFun.MAX (a.TotalFee)
    });

 11.Transaction

  // Get the database context 
  the ISession the session = null ;
   the try
  {
      // open transaction 
      session.Open ( to true );
       // SQL
       // commit the transaction 
      session.Commit ();
  }
  catch (Exception)
  {
      session?.Rollback();
      throw;
  }
  finally
  {
      session?.Close();
  }

 

Guess you like

Origin www.cnblogs.com/chaeyeon/p/11028480.html