Dapper.Contrib扩展介绍

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

T Get(id);
IEnumerable GetAll();
int Insert(T obj);
int Insert(Enumerable list);
bool Update(T obj);
bool Update(Enumerable list);
bool Delete(T obj);
bool Delete(Enumerable 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>();
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值