我正在使用 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/