Many thanks for your work on the amazing Z.EntityFramework.Plus and Z.EntityFramework.Classic.
Currently I'm using EDMX and EntityFramework 6.4.4 with Z.EntityFramework.Plus.EF6 and everything works great.
Currently I have to migrate my project to new .NET 7 and I have to do this incrementally. In first step I want to compile my project to .NET Standard 2.0, so I need to use Z.EntityFramework.Classic. I changed the nugets, my project compiles fine, but I got exception at runtime.
System.InvalidCastException: Unable to cast object of type 'System.Data.Entity.Core.Objects.ObjectQuery`1[DatabaseAPI.User]' to type 'System.Data.Entity.Infrastructure.DbQuery`1[DatabaseAPI.User]'.
at EntityFrameworkClassicExtensions.AsDbQuery[TQuery](IQueryable`1 this)
at System.Data.Entity.QueryableExtensions.Include[TResult,TProperty](IQueryable`1 source, Expression`1 path)
at CommandCenter.Repositories.UsersRepository.GetQueryable() in C:\code\CommandCenter\CommandCenterInfrastructure\CommandCenter.Repositories\UsersRepository.cs:line 83
at CommandCenter.Repositories.GenericRepository`2.Get(Expression`1 predicate) in C:\code\CommandCenter\CommandCenterInfrastructure\CommandCenter.Repositories\GenericRepository.cs:line 91
at CommandCenter.Services.UsersProvider.Get(String login, Boolean ignoreCase) in C:\code\CommandCenter\CommandCenterDomain\CommandCenter.Services\UsersProvider.cs:line 93
at WebAPI.Filters.Validators.Utils.HttpRequestSignatureValidator.Validate(HttpRequestMessage request) in C:\code\CommandCenter\WebAPI\Filters\Validators\Utils\HttpRequestSignatureValidator.cs:line 99
at WebAPI.Filters.Validators.ApiUserAuthorizeAttribute.DoesValidRequestSignatureExist(HttpActionContext context) in C:\code\CommandCenter\WebAPI\Filters\Validators\ApiUserAuthorizeAttribute.cs:line 109
at WebAPI.Filters.Validators.ApiUserAuthorizeAttribute.CustomOnAuthorization(HttpActionContext context) in C:\code\CommandCenter\WebAPI\Filters\Validators\ApiUserAuthorizeAttribute.cs:line 84
at WebAPI.Filters.Validators.CustomAuthorizeAttribute.OnAuthorization(HttpActionContext context) in C:\code\CommandCenter\WebAPI\Filters\Validators\CustomAuthorizeAttribute.cs:line 58
at System.Web.Http.Filters.AuthorizationFilterAttribute.OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()
I'm using EDMX that generates my MyEntities
context:
public partial class MyEntities : ObjectContext
{
public ObjectSet<User> Users
{
get
{
if ((_Users == null))
{
_Users = base.CreateObjectSet<User>("Users");
}
return _Users;
}
}
private ObjectSet<User> _Users;
/// Other collections ...
}
Then I get data with Include() method:
public override IQueryable<User> GetQueryable()
{
return this.Entities.Users.AsQueryable()
.Include(i => i.UserPrivileges)
.Include(i => i.UserEmails)
.Include(i => i.UserSetting);
}
I've used DotPeek because I wanted to understand this problem and I saw the following implementation:
public static class QueryableExtensions
{
....
public static IIncludeDbQuery<TResult, TProperty> Include<TResult, TProperty>(
this IQueryable<TResult> source,
Expression<System.Func<TResult, TProperty>> path)
{
Check.NotNull<Expression<System.Func<TResult, TProperty>>>(path, nameof (path));
string path1;
if (!DbHelpers.TryParsePath(path.Body, out path1) || path1 == null)
throw new ArgumentException(System.Data.Entity.Resources.Strings.DbExtensions_InvalidIncludePathExpression, nameof (path));
return (IIncludeDbQuery<TResult, TProperty>) new IncludeDbQuery<TResult, TProperty>(source.Include<TResult>(path1).AsDbQuery<TResult>(), path1);
}
...
The Include()
method returns even different type. It should returns IQuerable<T>
but it returns IIncludeDbQuery<TResult, TProperty>
and that requires a lot of code changes after changing the library.
Then I've checked the code on master, and it looks different:
|
public static IQueryable<T> Include<T, TProperty>( |
|
this IQueryable<T> source, Expression<Func<T, TProperty>> path) |
|
{ |
|
Check.NotNull(source, "source"); |
|
Check.NotNull(path, "path"); |
|
|
|
string include; |
|
if (!DbHelpers.TryParsePath(path.Body, out include) |
|
|| include == null) |
|
{ |
|
throw new ArgumentException(Strings.DbExtensions_InvalidIncludePathExpression, "path"); |
|
} |
|
|
|
return Include(source, include); |
|
} |
I don't know why but it takes code from:
|
public IIncludeDbQuery<TQuery, TProperty> Include<TProperty>(Expression<Func<TPropertyCurrent, TProperty>> path) |
|
{ |
|
string include; |
|
if (!DbHelpers.TryParsePath(path.Body, out include) || include == null) |
|
{ |
|
throw new ArgumentException(Strings.DbExtensions_InvalidIncludePathExpression, "path"); |
|
} |
|
|
|
return new IncludeDbQuery<TQuery, TProperty>(Include(include), include); |
|
} |
I've checked the latest version of library 7.2.5 and 7.2.4
The same exception was created #53 but user close this issue because he move back to EF + EF.Plus.