DapperPlus是否有与Dapper匹配下划线命名的等效配置?
DapperPlus 下划线列名与C#属性名匹配的实现方式
DapperPlus本身没有提供和Dapper中Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;完全等效的独立配置,但可以通过以下两种方式实现相同的列名匹配效果:
复用Dapper的全局配置
由于DapperPlus是基于Dapper开发的扩展库,Dapper的全局类型映射配置会直接作用于DapperPlus。只需要在项目启动时设置Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;,就能让DapperPlus自动将数据库下划线列名(如user_name)匹配到C#的驼峰属性名(如UserName),无需额外配置DapperPlus。手动自定义实体映射
如果需要针对特定实体做单独的列名映射,可以使用DapperPlus的EntityMapper进行手动配置,示例代码如下:// 针对YourEntity实体配置属性与列名的映射 DapperPlusManager.Entity<YourEntity>() .Map(entity => entity.UserName, "user_name") .Map(entity => entity.EmailAddress, "email_address");这种方式适合需要自定义个别字段映射的场景,灵活性更高。
内容的提问来源于stack exchange,提问作者Tristan Diether Pine
相关产品推荐
相关产品推荐

