简体   繁体   中英

entity framework override property get

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM