Linq to Entities计算不返回结果

时间:2014-01-08 18:00:56

标签: entity-framework linq-to-entities

我有以下linq查询产生错误的sql( ProductId = PictureId )。我们刚刚切换到使用Devart Entity Developer来自动生成我们的POCO类,这个问题就出现了。映射看起来对我来说正确,此查询先前返回了正确的结果。映射看起来是错误的还是查询本身永远不正确?

使用Entity Framework 5.0。

查询:

var totalResults = _productRepository.Table.Where(a => a.Pictures.Any()).Count();

SQL:

SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    COUNT(1) AS [A1]
    FROM [dbo].[Product] AS [Extent1]
    WHERE  EXISTS (SELECT 
        1 AS [C1]
        FROM [dbo].[ProductPicture] AS [Extent2]
        WHERE [Extent1].[ProductId] = [Extent2].[PictureId] // this should be ProductId = ProductId
    )
)  AS [GroupBy1]

Fluent Mapping:

        modelBuilder.Entity<Picture>()
            .HasMany(p => p.Products)
                .WithMany(c => c.Pictures)
            .Map(manyToMany => manyToMany
                .ToTable("ProductPicture", "dbo")
                .MapLeftKey("ProductId")
                .MapRightKey("PictureId"));

图:

enter image description here

1 个答案:

答案 0 :(得分:1)

DbContext模板中关联映射的错误在Entity Developer的最新版本(5.7.276)中得到修复:http://forums.devart.com/viewtopic.php?f=32&t=28723