简体   繁体   English

EF代码优先-使用“ include” where子句进行映射

[英]EF Code First - Mapping with “Include” Where Clause

In my case I have a table with the field "IsToDelete" (bit/boolean) where we delete these entries during maintenance time. 在我的情况下,我有一个带有字段“ IsToDelete”(位/布尔值)的表,我们在维护期间删除了这些条目。 But regarding the actual aplication we do not process these entries, we ignore them always by using the where condition istodelete == false. 但是对于实际的应用程序,我们不处理这些条目,我们总是使用where条件istodelete == false来忽略它们。

We have several places where we use something like ".Include(x=>x.MyEntities) I know that some people made similar questions such as: EF: Include with where clause 我们在几个地方使用了类似“ .Include(x => x.MyEntities)之类的东西。我知道有些人也提出了类似的问题,例如: EF:包含where子句

However, my question is regarding another type of approach: there are really some places were its not so good idea to "create" a "temp" mapped object (as suggested in EF: Include with where clause ). 但是,我的问题是关于另一种类型的方法:确实有一些地方不是“创建”“临时”映射对象的好主意(如EF:Include with where子句中所建议)。

When we are defining the EF code first mapping (EntityTypeConfiguration). 当我们定义EF代码时,首先映射(EntityTypeConfiguration)。 We have something similar to: 我们有类似的东西:

this.Property(t => t.IsToDelete).HasColumnName("IsToDelete");

I am thinking of a solution more of the kind "SQL Views" but without really have a "view". 我正在考虑一种更多的“ SQL视图”解决方案,但实际上没有“视图”。 Can we do it in this "mapping" file or in some other way (ALWAYS apply this filter)? 我们可以在此“映射”文件中或以其他方式(始终应用此过滤器)来执行此操作吗?

PS Human error of forgetting to add the where condition will no longer occur. PS忘记添加where条件的人为错误将不再发生。

You can make the property setter protected in your Entity Model. 您可以在实体模型中使属性设置器受到保护。 Somethink like that: 像这样:

public class CompanyReference : EntityReference<CompanyReference>
{
    #region Public Properties

    /// <summary>
    /// name
    /// </summary>
    [StringLength(8)]
    [Required]
    public string Name
    {
        get;
        protected set;
    }

    /// <summary>
    /// displayname
    /// </summary>
    [StringLength(45)]
    public string DisplayName
    {
        get;
        protected set;
    }

    #endregion
}

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

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