简体   繁体   English

实体框架覆盖属性get

[英]entity framework override property get

I have an entity named Product, with a property of ProductCode. 我有一个名为Product的实体,具有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. 我想透明地在ProductCode属性上维护一个前缀,该前缀对于应用程序的其余部分不可见,但在实体中维护。

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? 这行得通,但是在获取对象时,如何覆盖ProductCode的获取以自动去除“ AAA”前缀?

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... 然后在需要前缀代码时使用它...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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