EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

Derived types not working with Include Optimized

Open Jonatthu opened this issue 8 years ago • 14 comments

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();
        }

Jonatthu avatar Feb 10 '17 21:02 Jonatthu

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

JonathanMagnan avatar Feb 11 '17 15:02 JonathanMagnan

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 avatar Feb 11 '17 16:02 JonathanMagnan

@JonathanMagnan Now is not giving me the error message thanks! but is not including the properties that I'm getting with the normal include, untitled

Jonatthu avatar Feb 13 '17 15:02 Jonatthu

@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 :)

Jonatthu avatar Feb 13 '17 16:02 Jonatthu

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 avatar Feb 13 '17 16:02 JonathanMagnan

@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 :)

Jonatthu avatar Feb 13 '17 16:02 Jonatthu

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 avatar Feb 13 '17 16:02 JonathanMagnan

@JonathanMagnan Oh got it let this open please :)

Jonatthu avatar Feb 13 '17 16:02 Jonatthu

@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

yehiaelsayed avatar Jun 12 '18 11:06 yehiaelsayed

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 ExtensionsBulk OperationsDapper PlusLinqToSql Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval FunctionSQL Eval Function

JonathanMagnan avatar Jun 12 '18 12:06 JonathanMagnan

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 avatar Jun 13 '18 02:06 JonathanMagnan

@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 👍

yehiaelsayed avatar Jun 13 '18 12:06 yehiaelsayed

Something major is coming ;) That's all I'm allowed to said at this moment

JonathanMagnan avatar Jun 13 '18 12:06 JonathanMagnan

I am very excited to see it, and I am sure it will be a great something

yehiaelsayed avatar Jun 13 '18 12:06 yehiaelsayed