0

In my case I have a table with the field "IsToDelete" (bit/boolean) where we delete these entries during maintenance time. But regarding the actual aplication we do not process these entries, we ignore them always by using the where condition 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

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).

When we are defining the EF code first mapping (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". Can we do it in this "mapping" file or in some other way (ALWAYS apply this filter)?

P.S. Human error of forgetting to add the where condition will no longer occur.

Community
  • 1
  • 1
Dryadwoods
  • 2,875
  • 5
  • 42
  • 72
  • I don't believe this is possible currently. I think you're going to have to do it the 'long' way and hide the related entities on your objects and expose a filtered version of it instead. – Drauka Mar 22 '14 at 12:22

1 Answers1

0

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
}
peter70
  • 1,053
  • 16
  • 31
  • 1
    How that will affect filtering of related entities during eager loading? – Sergey Berezovskiy Mar 21 '14 at 08:34
  • I think the properties of the proxy classes, will be created (readonly) so as if it were the query to a view – peter70 Mar 21 '14 at 09:18
  • 1
    Looks like you didn't get the question - OP wants related entities to be *filtered* when loaded – Sergey Berezovskiy Mar 21 '14 at 09:20
  • @SergeyBerezovskiy exactly – Dryadwoods Mar 21 '14 at 09:20
  • @Drywoods, you've written: I am thinking of a solution more of the kind "SQL Views" but without really have a "view". With the solution you have some kind of view – peter70 Mar 21 '14 at 09:24
  • @Dryadwoods about your question - it is not possible now with EF - You can take a look on similar question I asked this week http://stackoverflow.com/questions/22504006/get-subset-of-entity-navigation-properties There is even feature request exists in EF backlog for this feature – Sergey Berezovskiy Mar 21 '14 at 09:27
  • I believe in EntityTypeConfiguration, you can´t filter data – peter70 Mar 21 '14 at 09:28
  • Point #1: EF Code First does not use "real SQL Views", since the dbcontext is using a mapped class/entity to a DB table. Point #2: There are some options to "overcome" this limitation, but by using "custom mapped objects" we are then talking about 2 different ways to "query" that table. But my objective is for the dbcontext's entity to apply always my filter. – Dryadwoods Mar 21 '14 at 09:32