• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    dapper使用Insert或update时部分字段不映射到数据库

    发布者: 土豆服务器 | 发布时间: 2025-6-20 09:10| 查看数: 59| 评论数: 0|帖子模式

    我们在使用dapper的insert或update方法时可能会遇见一些实体中存在的字段但是,数据库中不存在的字段,这样在使用insert时就是抛出异常提示字段不存在,这个时候该怎么解决呢,下面一起看一下:

    示例实体

    这里我们假如 test字段在数据库中不存在
    1. [Table("DemoTable")]
    2. public class DemoTable:BaseEntity,ISoftDelete
    3. {
    4.     public bool isDelete { get; set; }
    5.     [Key]
    6.     [Comment("主键")]
    7.     public int id { get; set; }
    8.     [Comment("姓名")]
    9.     [MaxLength(20)]
    10.     public string name { get; set; }
    11.     [Comment("身份证号")]
    12.     [MaxLength(18)]
    13.     public string idCard { get; set; }
    14.     public string test { get; set; }
    15. }
    复制代码
    1.自己根据实体生成sql(相对复杂)

    这里我们可以通过反射获取实体的属性,去判断忽略不需要的字段
    1. private string GetTableName() => typeof(T).Name;
    2. public async Task<int> CreateAsync(T entity)
    3. {
    4.     try
    5.     {
    6.         using IDbConnection db = GetOpenConn();
    7.         var type = entity.GetType();
    8.         //在这里我们略过了 id 和test 字段,这样在生成sql时就不会包含
    9.         var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
    10.                             .Where(prop => !string.IsNullOrWhiteSpace(prop.GetValue(entity))&& prop.Name != "id" && prop.Name != "test ");
    11.         var columns = string.Join(", ", properties.Select(prop => prop.Name));
    12.         var values = string.Join(", ", properties.Select(prop => $"@{prop.Name}"));
    13.         var query = $"INSERT INTO {GetTableName()} ({columns}) VALUES ({values})";
    14.         return Convert.ToInt32(await db.QueryAsync(query, entity));
    15.     }
    16.     catch (Exception e)
    17.     {
    18.         throw new Exception(e.Message);
    19.     }
    20. }
    复制代码
    2.使用特性跳过属性

    使用特性的方式就非常简单粗暴啦,引用using Dapper.Contrib.Extensions;
    在不需要的映射的属性上添加[Write(false)]
    1. using Dapper.Contrib.Extensions;
    2.         [Write(false)]
    3.         public int id { get; set; }
    4.         [Write(false)]
    5.     public string test { get; set; }
    6. using Dapper.Contrib.Extensions;
    7.         [Write(false)]
    8.         public int id { get; set; }
    9.         [Write(false)]
    10.     public string test { get; set; }
    复制代码
    然后直接调用Insert方法即可
    1. public async Task<int> CreateAsync(T entity)
    2. {
    3.     try
    4.     {
    5.         using IDbConnection db = GetOpenConn();
    6.                 return db.Insert<T>(entity);
    7.     }
    8.     catch (Exception e)
    9.     {
    10.         throw new Exception(e.Message);
    11.     }
    12. }
    复制代码
    到此这篇关于dapper使用Insert或update时部分字段不映射到数据库的文章就介绍到这了,更多相关dapper字段不映射到数据库内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/database/3078851ut.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    浏览过的版块

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表