1

在 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?

4

2 回答 2

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)
于 2013-07-11T09:01:35.913 回答
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])

于 2013-07-11T08:59:29.440 回答