How to put in a filter at the ObjectContext level, to remove "IsArchived" records?

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
EdB
Posts: 91
Joined: Mon 15 Oct 2012 09:24

How to put in a filter at the ObjectContext level, to remove "IsArchived" records?

Post by EdB » Tue 27 Aug 2013 12:29

Hi,

I need to simply prevent records marked with a "IsArchived" flag - true, to be excluded from any retrievals. I not want to change my LINQ queries. My Options seem to be:

1) Create a database View. Bad, as will require code changes to use this new object.
2) Add to "Where" clauses in application code. Bad, as will require lots of code writes.
3) Use of "Repository Pattern". Better, but still need to rewrite a significant chunk of code. Also there is a school of thought that says this is less needed now due to more modern ORM features.
4) Add filter to EF Code, most likely in ObjectContext codebase. I have seen this done with DbContext, but not "ObjectContext". THis seems a nice approach, but ED does overwrite this file. Also I get a casting error with the following code:

public ObjectSet<StdOrg> StdOrg
{
get
{
if ((_StdOrg == null))
{
_StdOrg = base.CreateObjectSet<StdOrg>("StdOrg");
_StdOrg = (ObjectSet<StdOrg>) _StdOrg.Where(r => r.IsArchived == false);
}
return _StdOrg;
}
}

What are your thoughts on this?

Many thanks.

EdB
Posts: 91
Joined: Mon 15 Oct 2012 09:24

Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?

Post by EdB » Tue 27 Aug 2013 16:34

Hi,

After more research, I think it has something to do with setting up a condition in the mapping details for an entity. However I cannot get it to work with my non nullable boolean IsArchived property.

IsArchived = 0
IsArchived = False
IsArchived = false

All above raise errors.

EdB
Posts: 91
Joined: Mon 15 Oct 2012 09:24

Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?

Post by EdB » Tue 27 Aug 2013 21:06

Hi,

Just worked out that one cannot use a mapped property in a condition. It can only exist as a storage column which creates 2 immediate issues:

1) When you try to update the data model, it thinks you have removed the column, by not mapping the property so wants to drop the column in the DB.

2) Since you cannot have it as a property, you can update it in the application.

So pretty perplexed about all of this at present.

Any assistance please.

EdB
Posts: 91
Joined: Mon 15 Oct 2012 09:24

Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?

Post by EdB » Tue 27 Aug 2013 22:14

Hi,

More research. Now working.

It seems that one cannot use a mapped property. Once I removed this, the condition worked. However this means that one cannot update this property via EF, and instead one has to use raw DDL via ExecuteStoreCommand. I think this is a EF restriction or EF/POCO restriction.

Any comment would be welcome.

EdB
Posts: 91
Joined: Mon 15 Oct 2012 09:24

Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?

Post by EdB » Wed 28 Aug 2013 09:35

Just another thought.

I think what is so confusing about this is that the Devart documentation on this seems incorrect, since it shows "unitscale" being a property and being used as a condition. I wish this were correct.

So this is either a code bug or a documentation bug ??

So which is it?

Thanks.

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?

Post by Shalex » Fri 30 Aug 2013 13:38

EdB wrote:It seems that one cannot use a mapped property. Once I removed this, the condition worked. However this means that one cannot update this property via EF, and instead one has to use raw DDL via ExecuteStoreCommand. I think this is a EF restriction or EF/POCO restriction.
Why don't you want to use a wrapper around your context?

Code: Select all

namespace Test
{
    public class Context
    {
        private TestMDataContext internalContext;

        public Context()
        {
            internalContext = new TestMDataContext();
        }

        public IQueryable<StdOrg> StdOrgs
        {
            get
            {
                return internalContext.StdOrgs.Where(r => r.IsArchived == false);
            }
        }
    }
}
EdB wrote:I think what is so confusing about this is that the Devart documentation on this seems incorrect, since it shows "unitscale" being a property and being used as a condition. I wish this were correct.

So this is either a code bug or a documentation bug ??
Please post a link to the article in our documentation you are talking about and specify the steps we should follow to reproduce the problem.

EdB
Posts: 91
Joined: Mon 15 Oct 2012 09:24

Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?

Post by EdB » Fri 30 Aug 2013 14:12

1. Ok, never thought to use a wrapper. So are you saying that I just need to wrap up my initial context with a 2nd context, and then add the filter code to the 2nd context, then address the 2nd context in my controllers?

2. The Help page I am accessing can be found by searching on "Entity Mapping " in help. Try and add a condition which uses a column which is mapped as a property like you show on this help page. It will not work, due to not being able to use mapped properties for conditions, only unmapped properties. I have version 5.5.89 .

Thanks,

Edward

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?

Post by MariiaI » Mon 09 Sep 2013 13:40

Ok, never thought to use a wrapper. So are you saying that I just need to wrap up my initial context with a 2nd context, and then add the filter code to the 2nd context, then address the 2nd context in my controllers?
Yes, this would be a more suitable solution for you.
It will not work, due to not being able to use mapped properties for conditions, only unmapped properties.
We couldn't reproduce this issue with the latest build of Entity Developer 5.5.185. Please try updating your version and notify us about the results.
Also, please send us the model you are working with so that we are able to investigate this issue in more details.

Post Reply