How can I configure Entity Framework to automatically trim values retrieved for specific columns mapped to char(N) fields?

后端 未结 6 1500
感动是毒
感动是毒 2020-12-02 14:30

I\'m working with a third-party database in which all text values are stored as char(n). Some of these text values are primary keys, whereas others are just nor

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 15:05

    Use properties with backing fields instead of automatic properties on your entities.

    Add the "Trim()" in the property setter, like so:

        protected string _name;
        public String Name
        {
            get { return this._name; }
            set { this._name = (value == null ? value : value.Trim()); }
        }
    

    I wrote my own POCO generator that just does this automatically, but if you don't have an option like that, ReSharper can add backing fields to automatic properties in like two keystrokes. Just do it for strings, and you can do a global (at the file scope) find/replace for " = value;" with "= value.Trim();".

提交回复
热议问题