C# 实体框架覆盖属性获取
我有一个名为Product的实体,其属性为ProductCode。我希望在ProductCode属性上透明地维护一个前缀,该前缀对应用程序的其余部分不可见,但在实体中维护 我可以这样做来设置前缀:C# 实体框架覆盖属性获取,c#,entity-framework,C#,Entity Framework,我有一个名为Product的实体,其属性为ProductCode。我希望在ProductCode属性上透明地维护一个前缀,该前缀对应用程序的其余部分不可见,但在实体中维护 我可以这样做来设置前缀: partial void OnProductCodeChanged() { if (EntityState != System.Data.EntityState.Detached) { if (this.ProductCode.Lengt
partial void OnProductCodeChanged()
{
if (EntityState != System.Data.EntityState.Detached)
{
if (this.ProductCode.Length == 11)
{
this.ProductCode = "AAA" + this.ProductCode;
}
}
}
这是可行的,但是如何覆盖产品代码的get,以便在获取对象时自动去除“AAA”前缀?为什么不添加这样的内部属性
internal string InternalProductCode
{
get
{
return String.Format("AAA-{0}",this.ProductCode);
}
}
然后在需要前缀代码时使用它…为什么不添加这样的内部属性
internal string InternalProductCode
{
get
{
return String.Format("AAA-{0}",this.ProductCode);
}
}
然后在需要前缀代码时使用它…我需要前缀值实际位于数据库中,以便以后导出。我目前只是用另一个neame为属性添加别名,以便于读取,但希望有一种更简洁的方法。我需要前缀值实际位于DB中,以便以后导出。我目前只是用另一个neame对该属性进行别名处理,以便于读取,但希望有一种更整洁的方式。