-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Entity objects cannot be returned within arrays in EF 3.0 preview 9 #17744
Comments
cc @smitpatel what do you think about this? |
Probably 3.1 |
Work-around db.Parts.Select(x => new { x }).AsEnumerable().Select(t => new object[] { t.x }).ToList(); |
The queries are created dynamically at runtime via the Expression namespace, and (as far as I know) it isn’t possible to create an anonymous type at runtime. If it is possible, that would be its own workaround, and I would not need an object returned in an array. |
Tests to add in SimpleQueryTestBase.Select.cs [ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Projection_into_object_array(bool isAsync)
{
return AssertQuery(
isAsync,
ss => ss.Set<Customer>().OrderBy(c => c.CustomerID).Where(c => c.CustomerID.StartsWith("A"))
.Select(c => new object[] { c }),
entryCount: 4,
assertOrder: true);
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Projection_into_object_array_2(bool isAsync)
{
return AssertQuery(
isAsync,
ss => ss.Set<Order>().OrderBy(o => o.OrderID).Where(o => o.OrderID < 10300)
.Select(o => new object[] { o, o.Customer }),
entryCount: 87,
assertOrder: true);
} This is issue in entity equality rewrite. The entityReferenceExpression was not removed in the processing hence translation pipeline failed. Removing entityEquality makes it to pass so we have translation properly in place. |
@smitpatel @roji Does this also fix the ListInitExpression? It would seem that the code might also need to handle VisitListInit to fix my second use case, which is the following: var ret = db.Parts.Select(x => new Dictionary<string, object> { { "PartId", x.PartId }, { "Manufacturer", x.Manufacturer } }).ToList(); I'm guessing a test case would look something like this: [ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Projection_of_multiple_entity_types_into_dictionary(bool isAsync)
{
return AssertQuery(
isAsync,
ss => ss.Set<Order>().OrderBy(o => o.OrderID).Where(o => o.OrderID < 10300)
.Select(o => new System.Collections.Generic.Dictionary<string, object>() { { "obj", o }, { "cust", o.Customer } }),
entryCount: 87,
assertOrder: true);
} Thanks!!! |
This query throws an error:
Error message:
However, this works just fine: (and properly tracks the entity)
Where Myclass is defined as: (notice that Part is still defined as an object, so that's not the issue)
Also, selecting any non-entity works just fine:
For EF 2.x, the same bug existed, but the workaround was to use AsNoTracking(). This workaround does not work in EF 3 preview 9.
Stack trace as follows:
The problem exists when selecting any navigation property or returning any entity, and also occurs when returning dictionary objects. For example, this would cause the same issue: (assuming that Manufacturer was a navigation property)
I have not been able to find any workaround -- for example, this doesn't work:
FYI, my code uses expression builders to translate graphql queries into dynamic expression trees via nested Dictionary<string, object> objects.
Further technical details
EF Core version: 3.0 preview 9
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0 preview 9
Operating system: Win 10 Pro x64
IDE: Visual Studio 2019 16.1.3
The text was updated successfully, but these errors were encountered: