Entity Framework Core Linq query doesn't translate to correct SQL statements

entity-framework-core mysql sql vb.net windows

Question

So I am debugging our application which uses Entity Framework Core 2.1.14. I created my entities from database first by scaffolding. Database is MySQL 5.7.29, the SQL data provider for EF Core is Oracle's MySql.Data.EntityFrameworkCore 8.0.19.

Example: a simple Linq query should create a proper SQL statement in the backend. However it does not. "Inactive" is a Byte?.

Dim associates = (From a In DatabaseContext.Associate
                  Where a.Inactive = 0
                  Select a.AssociateId).ToList()

This simple Linq query returns: SELECT `a`.`Inactive`, `a`.`AssociateID` FROM `associate` AS `a`. So basically it downloads all associates and does the "where" in memory and returns the values. I know according to the documentation some queries can be complicated and can't be translated, but this is a simplest of the simple queries. Is it because the Inactive column is nullable? Something is terrible not right.

FYI: EF Core is in a class library that is C#. The VB code is in another project that is VB that references the class library.

1
0
4/14/2020 4:49:21 PM

Popular Answer

Hmm, seems the answer is to make the query more specific in VB. I guess the query conversion between C# class library and VB project seems to be the issue.

Dim associates = (From a In DatabaseContext.Associate
                  Where a.Inactive.Value = Convert.ToByte(0)
                  Select a.AssociateId).ToList()

This provides the correct query:

SELECT `a`.`AssociateID`
FROM `associate` AS `a`
WHERE `a`.`Inactive` = 0

Very weird that in C# you don't have to check against the .Value and convert the 0 to a byte but in VB I had to do it, there was no other way to get it work. Thanks for everyone trying to help with this.

0
4/14/2020 6:01:08 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow