Dapper.Contrib扩展介绍

Dapper.Contrib扩展介绍

简介

Dapper.Contrib提供一系列关于增删查改扩展方法,目前有以下方法:

T Get<T>(id);
IEnumerable<T> GetAll<T>();
int Insert<T>(T obj);
int Insert<T>(Enumerable<T> list);
bool Update<T>(T obj);
bool Update<T>(Enumerable<T> list);
bool Delete<T>(T obj);
bool Delete<T>(Enumerable<T> list);

属性

  • [Table("Tablename")] - 自定义表名属性,如果没有此属性该实体对应表名是类名+s,示例:User实体不加该属性,则对应表名是(Users)

      [Table("dbo.[User]")]
      public class User
      {
          [Key]
          int UserId { get; set; }
          string UserName { get; set; }
          int Age { get; set; }
      }
    
  • [Key] - 自增列键属性,对应数据库表中的自增列,表数据插入时忽略该字段

      public class User
      {
          [Key]
          int UserId { get; set; }
          string UserName { get; set; }
          int Age { get; set; }
      }
    
  • [ExplicitKey] - 非自增列键属性,对应数据库表中的非自增列,表数据插入,需给该字段赋值

      public class User
      {
          [ExplicitKey]
          int UserId { get; set; }
          string UserName { get; set; }
          int Age { get; set; }
      }
    
  • [Write(true/false)] - 表示插入和更新,此字段是否写入

      public class User
      {
          [Key]
          int UserId { get; set; }
          string UserName { get; set; }
      	[Write(false)]
          int Age { get; set; }
      }
    

实体字段设置[Write(false)],插入和更新会忽略该字段,下面是Age字段设置[Write(false)]后生成的插入和更新脚本,该脚本中不包含列Age

生成插入脚本

	exec sp_executesql
	N'insert into dbo.[User] 
	([UserName]) 
	values 
	(@UserName);
	select SCOPE_IDENTITY() id',
	N'@UserName nvarchar(4000)',
	@UserName=N'用户名'

生成更新脚本

	exec sp_executesql 
	N'update dbo.[User] 
	set [UserName] = @UserName 
	where [UserId] = @UserId',
	N'@UserId int,@UserName nvarchar(4000)',
	@UserId=1,
	@UserName=N'用户名1'
  • [Computed] - 插入和更新会忽略该字段,与[Write(true/false)]属性区别,还得继续找资料看看

注意:

一个实体下 [Key][ExplicitKey]属性只能存在一个,要么只存在[Key],要么存在[ExplicitKey];若两个属性都不存在,且实体中存在字段Id(不区分大小写),字段Id默认具备属性[Key];若[Key][ExplicitKey]属性不存在,且实体不包含字段Id,调用Insert<T>方法可能会报错,调用Update<T>Get<T>Delete<T>方法肯定报错,原因是没有Where条件;

方法

public class Car
{
    public int Id { get; set; } // 这个字段默认是键属性
    public string Name { get; set; }
}
  • 查询方法

    根据Id查询单个实体

      var car = connection.Get<Car>(1);
    

    生成的执行SQL脚本

      exec sp_executesql 
      N'select * from Cars where Id = @id',
      N'@id int',
      @id=1
    

    查询表中所有数据

      var cars = connection.GetAll<Car>();
    
  • 插入方法

    插入单条数据

      connection.Insert(new Car { Name = "Volvo" });
    

    生成的执行脚本

      exec sp_executesql 
      N'insert into Cars ([Name]) values (@Name);
        select SCOPE_IDENTITY() id',
      N'@Name nvarchar(4000)',
      @Name=N'Volvo'
    

    插入多条数据

      List<Car> cars = new List<Car> {
              new Car { Id = 0, Name = "Volvo" },
              new Car { Id = 0, Name = "Volvo1" }
          };
      connection.Insert(cars);
    

    生成的执行脚本

      --第一条数据脚本
      exec sp_executesql 
      N'insert into Cars ([Name]) values (@Name);
        select SCOPE_IDENTITY() id',
      N'@Name nvarchar(4000)',
      @Name=N'Volvo'
      --第二条数据脚本
      exec sp_executesql 
      N'insert into Cars ([Name]) values (@Name)',
      N'@Name nvarchar(4000)',
      @Name=N'Volvo1'
    

    执行语句监控

    图片alt

    插入多条数据时,只进行了一次数据库连接

  • 修改方法

    修改单条数据

      connection.Update(new Car() { Id = 1, Name = "Saab" });
    

    生成的执行脚本

      exec sp_executesql 
      N'update Cars set [Name] = @Name where [Id] = @Id',
      N'@Id int,
      @Name nvarchar(4000)',
      @Id=1,
      @Name=N'car Update'
    

    修改多条数据

      connection.Update(cars);
    
  • 删除方法

    删除Id为1的数据

      connection.Delete(new Car() { Id = 1 });
    

    生成的执行脚本

      exec sp_executesql 
      N'delete from Cars where [Id] = @Id',
      N'@Id int',
      @Id=1
    

    删除表中所有数据

      connection.DeleteAll<Car>();
    

参考:Dapper Tutorial
       Dapper.Contrib

代码:示例代码

posted @ 2018-09-28 17:41  山中自有人  阅读(651)  评论(0)    收藏  举报