实体框架覆盖属性get

时间:2011-01-13 16:04:26

标签: c# entity-framework

我有一个名为Product的实体,其属性为ProductCode。我想在ProductCode属性上透明地维护一个前缀,该属性对应用程序的其余部分是不可见的,但是在实体中维护。

我可以这样设置前缀:

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

这样可行,但是如何在获取对象时覆盖ProductCode的get以自动删除“AAA”前缀?

1 个答案:

答案 0 :(得分:1)

为什么不添加像这样的内部属性

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

然后在需要前缀代码时使用它...