c# - 使用 Dapper 映射时自动修剪列中的空白

标签 c# ado.net dapper

我正在使用 Dapper 访问旧数据库,并且列上有空白填充。

我找到的解决方案是添加如下属性:

private string _status;
public string status 
{
    get { return _status; }
    set { _status = value.Trim(); }
}

显然,对将与该数据库交互的每个模型的每个字符串成员执行此操作是很乏味的。根据MSDN Entry on SET ANSI_PADDING ,将其修改为 OFF 也不是一个选项。

由于数据库不是我的,我无法将其更改为 nvarchar 或 simiar。

是否有一种简单的方法可以强制 Dapper 修剪从数据库读取的每个字符串列?

最佳答案

看看Dapper.Contrib 。如果返回的实例是接口(interface)类型,则 Get 方法可以为每个返回的实例生成更改跟踪代理。

(来自 Get 方法的注释)

Returns a single entity by a single id from table "Ts". T must be of interface type. Id must be marked with [Key] attribute. Created entity is tracked/intercepted for changes and used by the Update() extension.

您可以修改 CreateProperty 方法来实现您的 Trim 逻辑

关于c# - 使用 Dapper 映射时自动修剪列中的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18095351/

相关文章:

Dapper 执行存储过程引发关于多映射的 ArgumentException

c# - ASP.NET-CORE 2.2 SignalR CORS

c# - 计算器在一行上使用所有输入数据

c# - 优雅地终止由 QueueUserWorkItem() 分派(dispatch)的线程池任务

entity-framework - Entity Framework 4.1 : The navigation property 'BusinessUser' declared on type 'Login' has been configured with conflicting multiplicities

sql - 使用 ADO.NET 和 AddWithValue() 处理空值

c# - 如何测试 DataSet 是否为空?

c# - Decimal 类型从 In-memory Sqlite 数据库中读取为 double 或 int

c# - 使用 Dapper 查询复杂对象

c# - Mvvm Light 和 Visual C# Express?