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...
精彩评论