Entity framework Linq到实体计数不返回结果
我有以下linq查询,它生成了错误的sql(ProductId=PictureId)。我们刚刚切换到使用Devart Entity Developer自动生成POCO类,这个问题就出现了。映射在我看来是正确的,此查询以前返回了正确的结果。映射看起来是错误的还是查询本身从来都不正确 使用实体框架5.0 查询:Entity framework Linq到实体计数不返回结果,entity-framework,linq-to-entities,Entity Framework,Linq To Entities,我有以下linq查询,它生成了错误的sql(ProductId=PictureId)。我们刚刚切换到使用Devart Entity Developer自动生成POCO类,这个问题就出现了。映射在我看来是正确的,此查询以前返回了正确的结果。映射看起来是错误的还是查询本身从来都不正确 使用实体框架5.0 查询: var totalResults = _productRepository.Table.Where(a => a.Pictures.Any()).Count(); SELECT [
var totalResults = _productRepository.Table.Where(a => a.Pictures.Any()).Count();
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"));
SQL:
var totalResults = _productRepository.Table.Where(a => a.Pictures.Any()).Count();
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"));
Fluent映射:
var totalResults = _productRepository.Table.Where(a => a.Pictures.Any()).Count();
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"));
modelBuilder.Entity()
.HasMany(p=>p.Products)
.有许多(c=>c.图片)
.Map(manytomy=>manytomy
.ToTable(“ProductPicture”、“dbo”)
.MapLeftKey(“产品ID”)
.MapRightKey(“PictureId”);
图表:
var totalResults = _productRepository.Table.Where(a => a.Pictures.Any()).Count();
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"));
在Entity Developer的最新版本(5.7.276)中,DbContext模板中关联映射的错误已经修复。我认为mapkey方法应该是另一种方法。左键为PictureId,右键为ProductId?您的右键。我以前试过,但没用。清洗溶液并再次翻转后,它开始工作。看起来EntityDeveloper的代码生成脚本完全错了。谢谢