Derived types not working with Include Optimized
I'm using EF6
The next code is from my generic repository
This is currently working with the normal Include from entity framework but when I use
query.IncludeOptimized(includeProperty). this just give me the next error message
An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code
Additional information: There are no EntitySets defined for the specified entity type 'Mapwords.Models.Client'. If 'Mapwords.Models.Client' is a derived type, use the base type instead.
User is my main Class and Client is a derived type but is working fine with Include from entity, Why is this happening?
public virtual IQueryable<T> GetQuery(Expression<Func<T, bool>> filter = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = this.dbSet.AsNoTracking();
if (filter != null)
query = query.Where(filter).AsNoTracking();
foreach (var includeProperty in includeProperties)
//query = query.IncludeOptimized(includeProperty).AsNoTracking();
query = query.Include(includeProperty).AsNoTracking();
if (orderBy != null)
return orderBy(query).AsNoTracking();
return query.AsNoTracking();
}
Hello @Jonatthu ,
We successfully reproduced this issue.
This is caused by our library (obviously) when trying to find the KeyMember from the type provided.
To retrieve the KeyMember, our library tries to create an ObjectSet from the type provided (Client) which cause the issue.
We are currently looking how we could loop on BaseType when this error occurs to use instead the "User" class to create the ObjectSet.
More information will be soon available.
Best Regards,
Jonathan
Hello @Jonatthu ,
The v1.4.27 is now available: https://www.nuget.org/packages/Z.EntityFramework.Plus.EF6/
Let me know if the issue is fixed.
Best Regards,
Jonathan
@JonathanMagnan Now is not giving me the error message thanks! but is not including the properties that I'm getting with the normal include,

@JonathanMagnan Seems like removing .AsNoTracking(); let includeOptimized and filter work but, with the normal include is not a problem, I don't mind but I would like to know why :)
Hello @Jonatthu ,
Sorry, I just see you use "AsNoTracking" and forget to mention you IncludeOptimized is only compatible when tracking is enabled.
I tried to support it this weekend but time was missed to find out the final solution.
I will try to support it next weekend, but I cannot make a promise on it.
Why it's not supported?
Simply because we let Entity Framework built all relations via the Change Tracker.
The current alternative solution will simply create a new Context and track theses relations in this new context instead. So the original context will not track these entities.
Best Regards,
Jonathan
@JonathanMagnan Thanks very much for the update and the explanation, now it is working for now without the AsNoTracking(); a great library for EF we hope to see it in action in EF Core soon :)
Since I will try to support it soon, better let this issue open until I support AsNoTracking (or abandon the support).
When EF Core will be more stable, more features will be for sure available ;)
@JonathanMagnan Oh got it let this open please :)
@JonathanMagnan I am facing an issue using "IncludeOptimized" function with EF 6 inheritance (Table Per Type ) ,I have the following hierarchy as an example :
public abstract class Profile
{
public Guid Id {get;set;}
public string Name {get;set;}
}
public class Page:Profile
{
public int LiksCount {get;set}
}
public class Group:Profile
{
public string About {get;set}
public Guid OwnerId{get;set;}
public User Owner {get;set}
}
public class User
{
public Guid Id {get;set;}
public string Name {get;set;}
}
and when I am writing a linq query like :
var pages =myDbContext.Set<Page>().IncluedOptimized(p=>p.Owner).FirstOrDefault()
the "IncluedOptimized" function throw this exception:
There are no EntitySets defined for the specified entity type 'XYZ'. If 'XYZ' is a derived type, use the base type instead. Parameter name: TEntity'
after some debugging I found the exception occur in "QueryIncludeOptimizedProvider" class when the "Excute" function trying to get the member keys of the entity using "CreateObjectSet" method in "ObjectContext" class
Hello @yehiaelsayed ,
Thank you for letting us known.
We will try to see if there is something we can do about it.
Best Regards,
Jonathan
Performance Libraries
context.BulkInsert(list, options => options.BatchSize = 1000);
Entity Framework Extensions • Bulk Operations • Dapper Plus • LinqToSql Plus
Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval Function • SQL Eval Function
Hello @yehiaelsayed ,
The v1.8.1 has been released with your fix.
Let me know if everything is working as expected.
Thank again for your contribution.
Best Regards,
Jonathan
@JonathanMagnan Thanks a lot for the update and your fast response, it works very well now. you made a great library, hope to see more of it 👍
Something major is coming ;) That's all I'm allowed to said at this moment
I am very excited to see it, and I am sure it will be a great something