Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I use instead of an Include?

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:

enter image description here

enter image description here

Notes the difference in case.

When I want to eager load Sager's Stamkartotek I use Include, but the Include doesn't load Stamkartotek:

enter image description here

** Update 1 **

I noticed this strange behavior - if I use any fields from Stamkartotek it joins correctly: enter image description here

But if I go and only retrieve the value of Stam_nr instead of the whole object - it gives me A instead of a:

enter image description here

Research so far:

  • The EF team knows about this problem - but have decided not to fix it.
  • This guy has the same problem only using code-first - no solution has been found

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:
enter image description here

like image 507
Jens Kloster Avatar asked Nov 19 '13 09:11

Jens Kloster


3 Answers

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

like image 175
Colin Avatar answered Dec 31 '22 19:12

Colin


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).

like image 42
Juan Avatar answered Dec 31 '22 18:12

Juan


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.

like image 28
Pawel Avatar answered Dec 31 '22 20:12

Pawel