zzzprojects / EntityFramework-Classic

Entity Framework Classic is a supported version of the latest EF6 codebase. It supports .NET Framework and .NET Core and overcomes some EF limitations by adding tons of must-haves built-in features.
https://entityframework-classic.net
Other
103 stars 27 forks source link

Include with InvalidCastException exception #53

Closed schuettecarsten closed 4 years ago

schuettecarsten commented 4 years ago

The following code leads to an InvalidCastException exception:

public ICollection<Entity> QueryBy(long tenantID)
{
         return this.DbContext.Entities
                  .Where(x => x.TenantID == tenantID)
                  .Include(x => x.OtherEntity)
                  .ToList();
}

Exception details:

'Das Objekt des Typs "System.Data.Entity.Core.Objects.ObjectQuery`1[CMT.DataAccess.Model.Entity]" kann nicht in Typ "System.Data.Entity.Infrastructure.DbQuery`1[CMT.DataAccess.Model.Entity]" umgewandelt werden.'
JonathanMagnan commented 4 years ago

Hello @schuettecarsten ,

Could you provide a full example of this issue?

We tried something similar but we don't get the error:

var products = context.Products.Where(x => x.ColumnInt > 2).Include(x => x.Category).ToList();

You can send it to: info@zzzprojects.com if you need to keep the source private

Best Regards,

Jonathan


  Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus   Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval FunctionSQL Eval Function

JonathanMagnan commented 4 years ago

Hello @schuettecarsten,

Since our last conversation, we haven't heard from you.

Could you provide a full example of this issue on our email: info@zzzprojects.com

Looking forward to hearing from you,

Jon

JonathanMagnan commented 4 years ago

Hello @schuettecarsten

A quick reminder that we are here to assist you.

Don't hesitate to provide a full example of this issue to our email: info@zzzprojects.com

Looking forward to hearing from you,

Jon

schuettecarsten commented 4 years ago

Problem was caused by our own code. We used the following method to initiate a LINQ query, which works fine with EF6, but fails with EFClassic:

/// <summary>
/// Returns an <see cref="T:System.Linq.IQueryable`1" /> that allows to query the database.
/// </summary>
public override IQueryable<TEntity> Query<TEntity>()
{
    ObjectContext objectContext = ((IObjectContextAdapter)this.dbContext).ObjectContext;
    ObjectSet<TEntity> query = objectContext.CreateObjectSet<TEntity>();
    query.MergeOption = this.mergeOption;
    return query;
}

Meanwhile, I moved back to EF6 with EF+ because of too many side effects.