How to put in a filter at the ObjectContext level, to remove "IsArchived" records?
How to put in a filter at the ObjectContext level, to remove "IsArchived" records?
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.
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.
Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?
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.
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.
Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?
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.
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.
Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?
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.
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.
Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?
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.
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.
Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?
Why don't you want to use a wrapper around your context?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.
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);
}
}
}
}
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 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 ??
Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?
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
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
Re: How to put in a filter at the ObjectContext level, to remove "IsArchived" records?
Yes, this would be a more suitable solution for you.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?
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.It will not work, due to not being able to use mapped properties for conditions, only unmapped properties.
Also, please send us the model you are working with so that we are able to investigate this issue in more details.