Ofer Gal
Ofer Gal

Reputation: 883

calculated property in EF 6 Database first

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

Upvotes: 0

Views: 510

Answers (1)

Fabio
Fabio

Reputation: 11990

If you were using code-first, you could create a NotMapped property to return the concatenation of the values. Like this:

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

However, you are using database-first, so I think the better way is using a partial class that contains your property (it must be in the same namespace and assembly as generated part). Like this:

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

Upvotes: 1

Related Questions