You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an issue with mapping an EF Core 7 Json column into an class. I am presented with the following exception.
System.InvalidOperationException: The LINQ expression 'JsonQueryExpression(p.Addresses, $.AddressList)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
The code below is the projection that is mapping the queried AddressesData class to the Addresses class. (Which the Addresses is an EF Core 7 Json column, see DBContext at the bottom)
publicstaticclassAddressesDataExpressions{publicstaticclassProjections{privatestaticExpression<Func<AddressesData,Addresses>>Projection(){return a =>new(){AllAddresses=a.AddressList.AsQueryable().AsEnumerable().Select(ad =>newAddress{City=ad.City,CountryCode=ad.CountryCode,FirstLine=ad.FirstLine,PostCode=ad.PostCode,SecondLine=ad.SecondLine}).ToList(),PrimaryAddressIndex=a.Primary};}privatestaticFunc<AddressesData,Addresses>?_project;[Expandable(nameof(Projection))]publicstaticAddressesProject(AddressesDatadata){_project??=Projection().Compile();return_project(data);}}}
However, as you can see in the above code, I'm also using a projection for Privacy which I've converted back to an original Relational Database format with a table instead of Json column to test this issue, and this works without any issues.
I was just wondering if there's currently no support for EF Core 7 Json columns?
Below is the AddressesData database model class & the Addresses it is being mapped into.
publicclassAppDbContext:DbContext{publicAppDbContext(DbContextOptions<AppDbContext>options):base(options){}publicDbSet<ResourceData>Resources{get;set;}publicDbSet<DepartmentData>Departments{get;set;}=null!;publicDbSet<PersonData>People{get;set;}=null!;publicDbSet<StaffMemberData>StaffMembers{get;set;}=null!;publicDbSet<CustomerData>Customers{get;set;}=null!;publicDbSet<CustomerPrivacyData>CustomerPrivacyData{get;set;}=null!;protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<DepartmentData>().OwnsOne(p =>p.Address, options =>options.ToJson());// Have to use TPH if we're using a base class with JSON columns, TPC is not currently supportedmodelBuilder.Entity<PersonData>().OwnsOne(p =>p.SocialMedia, options =>options.ToJson());modelBuilder.Entity<PersonData>().OwnsOne(p =>p.Addresses, builder =>{builder.ToJson();builder.OwnsMany(a =>a.AddressList);});modelBuilder.Entity<StaffMemberData>().OwnsMany(p =>p.Certifications, options =>options.ToJson());modelBuilder.Entity<StaffMemberData>().OwnsMany(p =>p.Titles, options =>options.ToJson());//modelBuilder.Entity<CustomerData>().OwnsOne(p => p.Privacy, options => options.ToJson());}}
Thanks in advance!
The text was updated successfully, but these errors were encountered:
JackPSmith
changed the title
EF Core Json column projections - LINQ expression could not be translated
EF Core 7 Json column projections - LINQ expression could not be translated
Apr 13, 2023
Just a first impression: .AsQueryable().AsEnumerable() looks suspicious. Generally EF Core is only able to translate Expression-stuff, as used by the IQueryable-methods; IEnumerable on the other hand uses compiled Funcs, that aren't easily translated. Can you remove .AsQueryable().AsEnumerable() and see, if it changes anything? If the compiler objects, try removing only .AsEnumerable() or explicitly casting the lambda inside the Select to Expression<Func<..., Address>> (replacing ... by the type of items in AddressList).
I still receive the same error after removing .AsQueryable().AsEnumberable() and also removing just .AsEnumerable()
However, for your suggestion of explicitly casting the lambda expression, could you elaborate a little more as I'm afraid I'm not following you. In my case it would be Expression<Func<AddressData,Address>>
Hey Guys,
I have an issue with mapping an EF Core 7 Json column into an class. I am presented with the following exception.
System.InvalidOperationException: The LINQ expression 'JsonQueryExpression(p.Addresses, $.AddressList)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
The code below is the projection that is mapping the queried
AddressesData
class to theAddresses
class. (Which theAddresses
is an EF Core 7 Json column, see DBContext at the bottom)Below is the method that contains the EF Query
However, as you can see in the above code, I'm also using a projection for
Privacy
which I've converted back to an original Relational Database format with a table instead of Json column to test this issue, and this works without any issues.I was just wondering if there's currently no support for EF Core 7 Json columns?
Below is the
AddressesData
database model class & theAddresses
it is being mapped into.And here's the EF Db context config too
Thanks in advance!
The text was updated successfully, but these errors were encountered: