简体   繁体   中英

Eliding computed properties on EF6 Code First DbSet from database

I am trying to write a model for use within EF6 which includes a property that I would like to calculate in C# (not SQL). My model looks like this:

public class UploadRequestWorkingHours : UploadRequest
{
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public int WorkingHoursSinceResponseDate
    {
        get { return DateUtils.WorkingHoursSince(ResponseDate); }
        private set { }
    }
}

The UploadRequest class contains the details, and this derived class introduces a property that should be calculated by LINQ/EF/whomever when the model is serialized.

Unfortunately, this property — WorkingHoursSinceResponseDate — is making it into the database, even though I have marked the class as Computed . I have also tried None . I understand from reading the EF documentation that this DatabaseGeneratedOption will still create a column in the database; I have been following posts such as this one that have lots of votes, but in reality, I don't think this is the solution.

How to I prevent a computed property in the model from making it into the DB? Another post suggests creating the migration and commenting out the field in the migration file created but this is very smelly and there's no way I'd get away with this!

Found it: [NotMapped]

Thanks anyway!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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