我可以使用什么而不是包含?

时间:2022-03-30 13:46:15

Is there an alternative to using Include to eager load entities?

是否有使用Include到预先加载实体的替代方法?

The reason I can't use Include is that it appears to be case sensitive.
Consider the following example:
I have two tables:

我不能使用Include的原因是它似乎区分大小写。请考虑以下示例:我有两个表:

我可以使用什么而不是包含?

我可以使用什么而不是包含?

Notes the difference in case.

注意案例的区别。

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

当我想要加载Sager的Stamkartotek时,我使用Include,但Include不加载Stamkartotek:

我可以使用什么而不是包含?

** Update 1 **

I noticed this strange behavior - if I use any fields from Stamkartotek it joins correctly: 我可以使用什么而不是包含?

**更新1 **我注意到这种奇怪的行为 - 如果我使用Stamkartotek中的任何字段,它会正确加入:

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

但是,如果我去,只检索Stam_nr的值而不是整个对象 - 它给了我一个而不是一个:

我可以使用什么而不是包含?

Research so far:

迄今为止的研究:

  • The EF team knows about this problem - but have decided not to fix it.
  • EF团队知道这个问题 - 但决定不解决它。

  • This guy has the same problem only using code-first - no solution has been found
  • 这个人只有使用代码优先才有同样的问题 - 没有找到解决方案

Update 2
SQL genereted with Include:

更新2包含Include的SQL:

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:
我可以使用什么而不是包含?

更新3将它们加载到单独的查询中,并让changetracker修复引用。它似乎也不起作用:

3 个解决方案

#1


3  

Create a view with LOWER around the foreign keys to ensure reads always return the same case to EF.

在外键周围创建一个LOWER视图,以确保读取总是将相同的大小写返回给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

.NET和SQL Server之间的字符串比较差异导致在状态管理器中解决FK关系的问题

#2


1  

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.

使用linq时,例如,当您调用ToList()或First时,查询将执行到数据库。如果使用Include,则在调用某些操作时,查询将加载数据。

The problem with the A or a can be a collation situation, check your configuration some collation ignore the case of the data.

A或a的问题可以是整理情况,检查一下您的配置某些排序规则忽略数据的情况。

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

我建议:使用整数键更新正在使用的表,如果要加载相关数据,请使用左外部联接。有时最好使用好的旧tsql(你也可以在linq中进行左外连接)。

#3


1  

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.

您可以在单独的查询中加载实体,而不是使用.Include。更改跟踪器将修复它已经跟踪的相关实体的关系,如果您的查询正确,您应该从功能角度获得一个解决方案,相当于使用.Include。

#1


3  

Create a view with LOWER around the foreign keys to ensure reads always return the same case to EF.

在外键周围创建一个LOWER视图,以确保读取总是将相同的大小写返回给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

.NET和SQL Server之间的字符串比较差异导致在状态管理器中解决FK关系的问题

#2


1  

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.

使用linq时,例如,当您调用ToList()或First时,查询将执行到数据库。如果使用Include,则在调用某些操作时,查询将加载数据。

The problem with the A or a can be a collation situation, check your configuration some collation ignore the case of the data.

A或a的问题可以是整理情况,检查一下您的配置某些排序规则忽略数据的情况。

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

我建议:使用整数键更新正在使用的表,如果要加载相关数据,请使用左外部联接。有时最好使用好的旧tsql(你也可以在linq中进行左外连接)。

#3


1  

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.

您可以在单独的查询中加载实体,而不是使用.Include。更改跟踪器将修复它已经跟踪的相关实体的关系,如果您的查询正确,您应该从功能角度获得一个解决方案,相当于使用.Include。