EF4 linq NullReferenceException on non null object - c#

I am using ef4 code first with a generic repository. My repository has a select method that looks like this:
public IEnumerable<T> Select(Func<T, bool> predicate)
{
return objectSet.Where(predicate);
}
I am calling it with the following code
pushQueueRepository.Select(x => x.User.ID == user.ID && x.PageID == pageID);
*note - pushQueueRepository has been properly instantiated.
When I run this I am getting a NullReferenceException. When I look at it in debug after the exception is thrown it shows the error being x.User.ID == user.ID. When I mouse over x.User it is null. However when I expand x there us a User object in x.User (not null) that does have an id.
FYI x is a PushQueue object that is defined as such:
public class PushQueue : IEntity
{
...
[Required]
public User User { get; set; }
...
}
This doesn't seem right, am I missing something?
Thanks.

The reason for getting the exception is because you are loading all of the PushQueues in memory and then trying to apply your predicate: x => x.User.ID == user.ID and because lazy loading is NOT enabled by your code, x.User will not be lazy loaded therefore the exception is being thrown. You've not mark User navigation property as virtual, so it didn't opt in to EF Lazy Loading. When expand it in debug mode in VS, you are explicitly loading it but at runtime it's not lazy loaded and that's why you see it's populated when you expand it.
To fix this you need to change the signature of your Select method as that's the main problem: you are passing Func<T, bool>, while it needs to be Expression<Func<T, bool>> instead. Basically you want your predicate to be executed in the data store and not in memory, so you need to change the code to this:
public IEnumerable<T> Select(Expression<Func<T, bool>> predicate)
{
return objectSet.Where(predicate);
}
Of course alternatively you can keep the select method as it is now and enable lazy loading, this way, the NullReferenceException would go away but it will result in horrible performance at runtime since EF will be trying to lazy load User on every single PushQueue object and then apply your predicate:
public virtual User User { get; set; }

It's quite possible that by expanding x, you're causing other properties to be evaluated which then populate x.User.
Of course if this is an EF repository I'd expect the actual query to be executed in the database anyway, so what you see in the case of a query failure is somewhat unusual anyway. Have you tried looking at what query is executing in SQL?

I think you need to use the .Include:
pushQueueRepository.Include("User").Select(x => x.User.ID == user.ID && x.PageID == pageID)
.Include forces EF to load the User object right away, which would otherwise be lazily loaded and thereby not available for your comparison on User.ID.
Having a string in the .Include is not very elegant and unfortunately not compile-time checked. You can do something like this:
pushQueueRepository.Include(typeof(User).Name)....
Not very elegant either, but at least it is checked by the compiler ;-)

Related

Does EF6 virtual navigation property result into SQL query?

Assuming that I have an entity with virtual nagivation property like this:
public class School
{
public long Id { get; set; }
public virtual ICollection<Students> Students { get; set; }
}
As I understand, EF6 uses proxy to enable lazy-loading of Students, but do the following LINQ queries:
var myStudent = this.Students.Single(x => x.Id == id);
var studentsCount = this.Students.Count();
var bestStudents = this.Students
.OrderByDescending(x => x.GPA)
.Take(5)
.ToArray();
result into a SQL query (just like IQueryable<T> does)? Or is it just a lazy-loading collection and will load all students into memory after the first request, and then perform simple IEnumerable<T> LINQ behaviour?
When you query for an entity in Entity Framework, the objects that get returned are not (always) the type of object you think they are. Behind the scenes, it creates a brand new class that inherits from the your class. Because OOP allows a subclass to be stored in a variable typed as the superclass, you never really notice. This is the "proxy" that you mention. That's why the virtual function allows lazy loading. The subclass overrides your virtual method, and contains the code to lazy load the extra data before returning it.
That overridden property call will then check the context to see if the navigation properties are already loaded. If they are, it just returns them. If they are not, it will make additional SQL calls to load them, storing them in the DbContext for later.
In your updated question, my understanding is that running those lines of code would result in 3 separate queries being executed.
Public virtual properties are lazy loading in EF6.
You can disable lazy loading for a DbContext, or use the .Include() method on the IQueryable to include the property in the first query.
http://www.entityframeworktutorial.net/EntityFramework4.3/lazy-loading-with-dbcontext.aspx
https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx
Once you "iterate" through the list (e.g. by calling the .Single(), .Count() or .ToArray() method), the query is executed and you have a in-memory list of your students. See https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/query-execution for more details about query execution.
The first example would result in 3 queries, where the first returns the student with the given Id, the second returns the student count and the third returns the first 5 students ordered desc by their GPA property.
Regarding DDD => You can use some Layered architecture, with ApplicationServices and DomainServices, where DomainServices perform the domain logic and ApplicationServices load / transform the data.
The https://aspnetboilerplate.com/ template is for example a good starting point for domain driven design.
Assuming that the second code block is executed somewhere INSIDE the scope of an instance of 'School' (so 'this' is an instance of 'School' - in my example below school19) then the following scenarios are possible:
A) You have loaded your instance of 'School' like this (Lazy loading enabled):
var school19 = dbContext.Set<School>()
.FirstOrDefault(school => school.Id == 19)
Then your 3 lines of code for accessing the property 'Students' will trigger a single additional database hit when
var myStudent = this.Students.Single(x => x.Id == id);
is executed, but no more database hits will occur with the subsequent two statements.
B) In case you have loaded your instance of 'School' like this (Lazy loading enabled):
var school19 = dbContext.Set<School>()
.Include(school => school.Students)
.FirstOrDefault(school => school.Id == 19)
Then your 3 lines of code for accessing the property 'Students' will not trigger an additional database hit.
C) If lazy loading was disabled, then
A) would result in a Null Reference Exception
B) would behave the same
As a last remark, if 'this' was a reference to an instance of the DBContext class, which has a property
public Set<School> Schools { get; set; }
then it would trigger 3 different database calls. But the result is different as this would be executed in the context of ALL schools, while my assumptions above only apply to a single school.

Querying through NHibernate using a Interface in Orchard CMS

I am storing records in order to link 2 part records together in Orchard (CMS).
The records will have a reference to the 2 items they are linking.
There are several tables of records doing this, and they all implement the same interface.
//One example of a record implementing the common interface
//This relation links a member to a home
public class HomeToMemberRelationRecord : IRelationResultable
{
public virtual int Id { get; set; }
[Aggregate]
public virtual MemberPartRecord MemberPartRecord { get; set; }
[Aggregate]
public virtual HomePartRecord HomePartRecord { get; set; }
//The interface is implemented here
//...
}
I am trying to query these records in a generic way using the interface.
I have a generic method accepting the type and resolving the IRepository from that type.
The problem is the properties I want to use in the query differ for each record.
On the above record I may want to get the Member's from a Home, however another record might be linking a Dog to a DogHouse and I want to find all Dog's in the DogHouse.
The UI element fetching the relations will resolve the repository when needed:
Resolve<IRepository<T>>();
So there is no way to know if the query should be:
.Where(x => x.HomePartRecord.ContentItemRecord.Id == id)
//or
.Where(x => x.DogHousePartRecord.ContentItemRecord.Id == id)
So the interface implemented by the relation records must define how they themselves are to be queried.
I tried using a method to return the property I needed to query, but NHibernate did not like that and served me a NotSupportedException.
// Method in the interface
int GetId();
// Attempt to call above method from a query.
var repo = _services.WorkContext.Resolve<IRepository<T>>(); //Constraint: where T : IMemberSearchResultable, new()
var relations = repo.Table.Where(x => x.GetRelevantId() == id);
I tried using a property in my interface instead, but NHibernate would simply look for the property on the record in the database (It only exists in the model).
// Attempt to use a property in the interface instead of a method.
int RelevantId { get; set; } //Usage: .Where(x => x.RelevantId == id)
So I tried building an expression:
(Spoiler: This also failed.)
public static Expression<Func<T, bool>> GetExpression<T>(string propertyName, int filterValue)
{
var parameter = Expression.Parameter(typeof(T));
var property = Expression.Property(parameter, propertyName);
var method = typeof(int).GetMethod("Equals", new[] { typeof(int) });
var body = Expression.Call(property, method, Expression.Constant(filterValue));
return (Expression<Func<T, bool>>)Expression.Lambda(body, parameter);
}
public Expression<Func<T, bool>> TestGet<T>(int id)
{
return GetExpression<T>("MemberPartRecord.ContentItemRecord.Id", id);
}
I have tried a number of approaches to naming the property, but can not find one that works.
I can of course build the specific query I need in each case without using an interface. Which leads me to believe it should be possible to build the same query using the interface.
Am I building the expression wrong, is it the wrong approach or is the entire idea far fetched?
Changing mappings
If changing the mapping (and probably the interface altogether) is an option, you should try mapping your entities as inheriting from your common interface, as illustrated in inheritance mapping documentation. It should works without a base class.
But this would mean your interface would define some HomeBase property (typed as a base class or another common interface) which would the mapped one, and which would exists as such in your entities.
Then you would add some specialized ConcreteHome or DogHouse property on your entities, not mapped, and casting the HomeBase to the concrete home.
Probable issues
Beware of proxies, such setting would probably force you to use lazy="no-proxy" or lazy="false" on HomeBase property mapping.
Moreover, a post writes this is not supported in Fluent mapping (I use .hbm files).
And on top of that, if you need querying the related Home specific properties (not belonging to HomeBase), you would then have a new trouble.
Going farer into runtime type inspection
You may instead go farer with your current approach. But this will require quite some tinkering.
The aim would be to get a more capable helper:
public static Expression<Func<T, bool>> GetExpression<T, U>(
Expression<Func<I, BaseHome>> interfaceHomePropertySelector, U filterValue)
The helper implementation should get the interface property memberInfo, and then infer the corresponding member in T entity. From that, it would construct the adequate expression.
For performances reasons, the resulting inferring should then be cached for reducing runtime cost of subsequent uses.
Dirty inferring
You may infer the concrete property in T by checking all its properties and take the first which is compatible with the interface property, while not having the same name as the interface property. This requires having only a single Home property in each of your entities.
Harder inferring
You may go a quite more elaborated way to get the right property: runtime evaluation of what get actually called when accessing the interface property on an instance of T. For that, you would need to use the same proxyfying approach than NHibernate uses for handling lazy-loading.
Instantiate a new dummy instance of a T proxy, instrumented for calling a callback of yours at each property access. Access the interface property. Your callback should fires at least twice : at interface property access, then at the concrete property access the interface implementation should do.
So in your callback you would then have to inspect the call-stack to check in which case you are, and infer the concrete property of T.
I am posting what I have so far as an answer, but I hope someone out there has a better solution:
I have managed to get a working solution for my specific problem, but it's not a perfect solution as it does not allow you to use references in your query.
I can basically only refer to properties that are present on the C# record mapped to the database.
This is the method I am currently using:
public static Expression<Func<T, bool>> GetExpression<T, U>(string propertyName, U filterValue)
{
var parameter = Expression.Parameter(typeof(T));
var predicate = Expression.Lambda<Func<T, bool>>(
Expression.Equal(Expression.Property(parameter, propertyName),
Expression.Constant(filterValue)), parameter);
return predicate;
}
And I call it this way:
GetExpression<T, U>("MemberPartRecord", rec);
The significant difference here is that I am now querying the MemberPartRecord rather than trying to access MemberPartRecord.ContentItemRecord.Id
For my current needs this is enough, but only because I am in a position to provide the record I need for the query.
I will not accept this answer in the hopes that someone can provide a complete answer that allows the use of referenced records in the query.

Telerik OpenAccess generating the same query for every function call

I'm using a Generic Repository like pattern to fetch data. There are 100+ entities so creating a separate repository for each is not really an option. here are a few functions from the same class:
public int Count(Func<TEntity, bool> x=null)
{
return x == null ?
mgr.CTX.GetAll<TEntity>().Count() :
mgr.CTX.GetAll<TEntity>().Where(x).Count();
}
public TEntity One(Func<TEntity, bool> x)
{
return mgr.CTX.GetAll<TEntity>().Where(x).Take(1).FirstOrDefault();
}
public IQueryable<TEntity> All(Func<TEntity, bool> x=null)
{
return x == null ?
mgr.CTX.GetAll<TEntity>() :
mgr.CTX.GetAll<TEntity>().Where(x).AsQueryable<TEntity>();
}
The problem is no matter which function is call, the Sql profiler shows the same
Select [columns] from [table]
I suppose when using Take(1) or Count() or Where() the query should be made accordingly using Count(), Top or Where clauses of Select but these functions have absolutely no effects on query generation. Apparently, every operation seems to be performed in memory after fetching all the data from server.
Guide me if there is something wrong with the way I'm accessing it or this is the normal behavior of telerik?
I believe you are victim of a subtle difference between definitions of LINQ extension method - in-memory ones use Func<> while SQL-bound use Expression<> as parameter type.
My suggestion is to change All(Func<TEntity, bool> x=null) to All(Expression<Func<TEntity, bool>> x=null)

Fluent syntax for IQueryable extension method?

I'll let the code speak for itself:
public interface ISoftDeletable {
bool IsDeleted {get; set;}
}
public static class Extensions {
public IQueryable<T> Active<T>(this IQueryable<T> q) where T : ISoftDeletable {
return q.Where(t => !t.IsDeleted);
}
}
public partial class Thing : ISoftDeletable {
...
}
...
var query = from tc in db.ThingContainers
where tc.Things.Active().Any(t => t.SatisfiesOtherCondition)
select new { ... }; // throws System.NotSupportedException
The error is:
LINQ to Entities does not recognize the method 'System.Collections.Generic.IQueryable`1[Thing] ActiveThing' method, and this method cannot be translated into a store expression.
You get the idea: I'd like a fluent kind of way of expressing this so that for any ISoftDeletable I can add on a 'where' clause with a simple, reusable piece of code. The example here doesn't work because Linq2Entities doesn't know what to do with my Active() method.
The example I gave here is a simple one, but in my real code, the Active() extension contains a much more intricate set of conditions, and I don't want to be copying and pasting that all over my code.
Any suggestions?
You have two unrelated problems in the code. The first one is that Entity Framework cannot handle casts in expressions, which your extension method does.
The solution to this problem is to add a class restriction to your extension method. If you do not add that restriction, the expression is compiled to include a cast:
.Where (t => !((ISoftDeletable)t.IsDeleted))
The cast above confuses Entity Framework, so that is why you get a runtime error.
When the restriction is added, the expression becomes a simple property access:
.Where (t => !(t.IsDeleted))
This expression can be parsed just fine with entity framework.
The second problem is that you cannot apply user-defined extension methods in query syntax, but you can use them in the Fluent syntax:
db.ThingContainers.SelectMany(tc => tc.Things).Active()
.Any(t => t.SatisfiesOtherCondition); // this works
To see the problem we have to look at what the actual generated query will be:
db.ThingContainers
.Where(tc => tc.Things.Active().Any(t => t.StatisfiesOtherCondition))
.Select(tc => new { ... });
The Active() call is never executed, but is generated as an expression for EF to parse. Sure enough, EF does not know what to do with such a function, so it bails out.
An obvious workaround (although not always possible) is to start the query at the Things instead of the ThingContainers:
db.Things.Active().SelectMany(t => t.Container);
Another possible workaround is to use Model Defined Functions, but that is a more involved process. See this, this and this MSDN articles for more information.
While #felipe has earned the answer credit, I thought I would also post my own answer as an alternative, similar though it is:
var query = from tc in db.ThingContainers.Active() // ThingContainer is also ISoftDeletable!
join t in db.Things.Active() on tc.ID equals t.ThingContainerID into things
where things.Any(t => t.SatisfiesOtherCondition)
select new { ... };
This has the advantage of keeping the structure of the query more or less the same, though you do lose the fluency of the implicit relationship between ThingContainer and Thing. In my case, the trade of works out that it's better to specify the relationship explicitly rather than having to specify the Active() criteria explicitly.

How to query EF through the PK when using a generic class?

I'm trying to implement a generic class that will interact with a generic repository, and all is fine except for when I have to deal with getting objects out of the repository.
I'm going to have a virtual method in the generic class which will receive an int and I want to use that int to form a query to the repository that gets objects by their primary key. I have a feeling I need to work with the EntityKey property in EF, but not too sure how.
Anyway, here's what I'm trying to do in code, I hope someone will have suggestions on how to accomplish what I want:
public virtual T Get(int PrimaryKey) {
this.Repository.Select(
t =>
(t.PRIMARYKEY == PrimaryKey)).Single();
}
I want to extend this class with more specialized classes, but since most of them only get their objects by querying the PK, it makes since to me to have a base method that can do it.
Thanks in advance for any suggestions!
UPDATE
So, here's where I've gotten with reflection, and I doubt its the proper way, but it somewhat works... I'm getting a NotSupportedException with the message LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object, System.Object[])' method, and this method cannot be translated into a store expression.. Although I understand what it says and why it's saying, I'm not sure how to overcome it when my code looks like this:
private readonly string TEntityName = typeof(T).Name;
public virtual T Get(
int PrimaryKey) {
return this.Repository.Select(
t =>
(((int)t.GetType().GetProperties().Single(
p =>
(p.Name == (this.TEntityName + "Id"))).GetValue(t, null)) == PrimaryKey)).Single();
}
Hoping that someone who knows how to use reflection, unlike me, can point me in the right direction. Thanks!
Retrieving an entity by a PK using EF requires an expression/predicate, like this:
Expression<Func<Order,bool>> predicate = x => x.OrderId == 1;
return ctx.Orders.Single(predicate);
There is no easy way (short of reflection or expression tree creation) to be able to dynamically create this predicate.
What you could do is accept the predicate as a parameter:
public virtual T Get(Expression<Func<T,bool>> predicate) {
this.Repository.Select(predicate).Single();
}
Also make sure you put some generic constraints on T (either at the class/method level).
http://msdn.microsoft.com/en-us/library/bb738961.aspx is way to go. Then use http://msdn.microsoft.com/en-us/library/bb738607.aspx, spend some time in debugger and your misson is completed.

Categories