开发者

entity framework override property get

开发者 https://www.devze.com 2023-02-04 18:53 出处:网络
I have an entity named Product, with a property of ProductCode.I would like to transparently maintain a prefix on the ProductCode property, which is invisible to the rest of the application, but is开发

I have an entity named Product, with a property of ProductCode. I would like to transparently maintain a prefix on the ProductCode property, which is invisible to the rest of the application, but is开发者_运维技巧 maintained in the entity.

I can do this to set the prefix:

partial void OnProductCodeChanged()
    {
        if (EntityState != System.Data.EntityState.Detached)
        {
            if (this.ProductCode.Length == 11)
            {
                this.ProductCode = "AAA" + this.ProductCode;
            }
        }
    }

This works, but how can I override the get of ProductCode to automatically strip the "AAA" prefix when the object is fetched?


Why not add an internal property like this

internal string InternalProductCode
{
     get
     {
          return String.Format("AAA-{0}",this.ProductCode);
     }
}

then use that when you need the prefixed code...

0

精彩评论

暂无评论...
验证码 换一张
取 消