执行两个类似的查询后,实体框架返回错误的数据

时间:2013-08-08 09:31:43

标签: linq entity-framework

我有两个类似的查询,第一个:

var activatedSerialNumbers = (from activation in entities.Activations
                              where !canceledActivationsIds.Contains(activation.Id)
                              where activation.CustomerId == customerId
                              join licenseConfiguration in entities.LicenseConfigurations
                                              on activation.Id equals licenseConfiguration.ActivationId
                              where licenseConfiguration.ProductId == productId
                              join activatedSerialNumber in entities.ActivatedSerialNumbers
                                              on activation.Id equals activatedSerialNumber.ActivationId
                             where deactivatedSams.All(dsn => dsn.ToLower() != 
                                              activatedSerialNumber.Name.ToLower())
                             select new SamWithLicense
                             {
                                     Name = activatedSerialNumber.Name,
                                     Features = licenseConfiguration.LicenseFeatures
                             }).ToList();

第二个:

var activationsForSam = (from activation in entities.Activations
                        where !canceledActivationsIds.Contains(activation.Id)
                        where activation.CustomerId == customerId
                        let activatedSerialNumbers = activation.ActivatedSerialNumbers
                                                    .Select(sn => sn.Name.ToLower())
                        where activatedSerialNumbers.Contains(loweredSn)
                        join licenseConfiguration in entities.LicenseConfigurations
                                             on activation.Id equals activatedProduct.ActivationId
                        select new SamWithLicense
                        {
                                   Name = selectedSerialNumber,
                                   Features = licenseConfiguration.LicenseFeatures
                        }).ToList();

在某些情况下,我会一个接一个地执行它们,在大多数情况下它可以正常工作,但有些情况下 - 不是。在第二次查询的结果中,计数器从另一行获取:

Visual Studio - 快速观看 enter image description here

SQL Management Studio enter image description here

我想这是EF缓存或smth的问题,但不知道如何正确修复它。

1 个答案:

答案 0 :(得分:0)

在您的第一个查询中,您将加入激活ID(PK)到LicenseConfigurations ActivationId(FK)

join licenseConfiguration in entities.LicenseConfigurations
on activation.Id equals licenseConfiguration.ActivationId

在第二个查询中,看起来您正在加入查询“activatedProduct”之外定义的值

join licenseConfiguration in entities.LicenseConfigurations
on activation.Id equals activatedProduct.ActivationId