2014-01-08 104 views
1

我有以下linq查詢生成錯誤的sql(ProductId = PictureId)。我們轉而使用Devart Entity Developer來自動生成我們的POCO類,並彈出此問題。映射對我來說看起來是正確的,並且此查詢以前返回了正確的結果。映射看起來是否錯誤,或者查詢本身是不正確的?Linq to Entities count不返回結果

使用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] 

流利映射:

 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

我認爲mapkey方法應該是相反的方法。左鍵爲PictureId,右鍵爲ProductId? –

+0

你的權利。我之前嘗試過,它不起作用。清潔溶液並翻轉後再開始工作。看起來像Entity Developer的代碼生成腳本顯然是錯誤的。謝謝! – TugboatCaptain

回答