简体   繁体   English

从数据库中删除EF6 Code First DbSet上的计算属性

[英]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). 我正在尝试编写一个在EF6中使用的模型,其中包括一个我想在C#中计算的属性(不是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. UploadRequest类包含详细信息,并且此派生类引入了一个属性,该属性应在模型序列化时由LINQ / EF / whomever计算。

Unfortunately, this property — WorkingHoursSinceResponseDate — is making it into the database, even though I have marked the class as Computed . 不幸的是,即使我将类标记为Computed ,该属性( WorkingHoursSinceResponseDate其放入数据库中。 I have also tried None . 我也尝试过None I understand from reading the EF documentation that this DatabaseGeneratedOption will still create a column in the database; 通过阅读EF文档,我了解到此DatabaseGeneratedOption仍将在数据库中创建一列; 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] 找到了: [NotMapped]

Thanks anyway! 不管怎么说,还是要谢谢你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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