calculated property in EF 6 Database first

时间:2015-09-01 23:05:09

标签: c# entity-framework

When I do

public string RequestIDFormated { get; set; }

I see the name RequestIDFormated in the output with null value;

When I replace it for

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

or

[global::System.Runtime.Serialization.DataMemberAttribute()] 

And

    public string RequestIDFormated
    {
        get
        {
            return RecordCreateDatetime.Year.ToString();
        }
        private set { /* needed for EF */ }
    }

it does not even show in the output I am using EF 6.1.3

1 个答案:

答案 0 :(得分:1)

如果您使用的是代码优先,则可以创建NotMapped属性以返回值的串联。像这样:

[NotMapped]
public string SomeProperty 
{
   get { return Property1 + Property2; } 
}

但是,您使用的是数据库优先,所以我认为更好的方法是使用包含您的属性的partial class(它必须与生成的部分位于相同的命名空间和程序集中)。像这样:

public partial class YourEntntiy
{
     public string MyNewProperty { get { return Property1 + Property2; } }
}