Is there an alternative to using Include
to eager load entities?
The reason I can't use Include
is that it appears to be case sensitive.
Consider the following example:
I have two tables:
Notes the difference in case.
When I want to eager load Sager
's Stamkartotek
I use Include
, but the Include
doesn't load Stamkartotek
:
** Update 1 **
I noticed this strange behavior - if I use any fields from Stamkartotek
it joins correctly:
But if I go and only retrieve the value of Stam_nr
instead of the whole object - it gives me A
instead of a
:
Research so far:
Update 2
SQL genereted with Include
:
FROM [dbo].[Sager] AS [Extent1]
INNER JOIN [dbo].[Stamkartotek] AS [Extent2] ON [Extent1].[Klient_Stam_nr] = [Extent2].[Stam_nr]
WHERE 'jek15' = [Extent1].[Sags_nr]
Update 3
Loading them in seperate queries, and letting changetracker fixup the reference. It doesn't seem to work either:
Create a view with LOWER around the foreign keys to ensure reads always return the same case to EF.
And do inserts and deletes using stored procedures
You can track and vote for the issue to be addressed here:
String comparison differences between .NET and SQL Server cause problems for resolving FK relationships in the state manager
I don't have all the information in this case, I think you have to update the relation of your table using integer keys to make the relation.
When using linq the query is going to execute to the database when you call a ToList() or First for example. If you use an Include, the query will load the data when some of this action is called.
The problem with the A or a can be a collation situation, check your configuration some collation ignore the case of the data.
I propose: Update the tables you are using with integer keys and use left outer joing if you want to load related data. Sometimes is better to use good old tsql(you can make the left outer join in linq too).
Instead of using .Include
you can load entities in separate queries. Change tracker will fix up relations for related entities it is already tracking and if you get your queries right you should get a solution that from the functional perspective is equivalent to using .Include
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With