在NHibernate中,当我们执行下面的查询时,为它生成的SQL将包含集合中每个元素的参数。如果对MSSQL Server执行查询,并且集合中有2k或更多元素,则会出现错误,因为sql引擎允许的最大参数为2k。
var bankaccounts = from b in this.unitOfWork.BankAccounts
where command.Ids.Contains(b.Id) // command.ids is an array with id's
select b;
很抱歉懒得用EntityFramework自己测试一下,但我看到其他开发人员编写这样的代码:EF cannot delete child object because of associationset
问题是EF将为这类查询生成哪些SQL?
答案 0 :(得分:2)
我已经为你看了SQL分析器。这就是实体框架中发生的事情(我认为这是wat NHibernate也会产品,但我不确定..):
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[AccountNo] AS [AccountNo],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent1].[IBAN] AS [IBAN],
[Extent1].[IsActive] AS [IsActive],
[Extent1].[Customer_Id] AS [Customer_Id]
FROM [dbo].[BankAccounts] AS [Extent1]
WHERE [Extent1].[Id] IN (4,5,6,7,9)
如您所见,它将变为WHERE .. IN ([the contains values])
答案 1 :(得分:2)
使用此查询,
var ids = context.Countries
.Take(10).Select(c => c.CountryId).ToList();
var offices = context.Offices.Where(o => ids.Contains(o.CountryId))
.ToList();
它将生成此SQL:
SELECT
[Extent1].[OfficeId] AS [OfficeId],
[Extent1].[CountryId] AS [CountryId],
[Extent1].[OfficeName] AS [OfficeName]
FROM [dbo].[Office] AS [Extent1]
WHERE [Extent1].[CountryId] IN (3,4,5,6,7,8,9,10,11,12)