Incorrect query generated by EF when using FullAuditedEntity
See original GitHub issueI’m upgrading from Abp 0.9.0 to 3.2.4. Two queries that were working fine previously are failing now. The root cause appears to be the FullAuditedEntity
base class.
The error being generated is: {"Unknown column 'Extent1.ParentId' in 'where clause'"}
, which after checking the query executed, it’s not using the foreign keys to do the joins and is producing a query that doesn’t work.
Assume the following three classes:
public class GrandParent : FullAuditedEntity
{
public string Name { get; set; }
}
public class Parent : FullAuditedEntity
{
public string Name { get; set; }
public int GrandParentId { get; set; }
public virtual GrandParent GrandParent { get; set; }
}
public class Child : FullAuditedEntity
{
public string Name { get; set; }
public int ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
Pretty simple setup with a one to many relationship up the chain. Assume the following function to retrieve data from the Child
repository:
var childQuery = _childRepository
.GetAll()
.Where(c => c.Parent.GrandParent.Name == "My Grandparent");
var children = await childQuery.ToListAsync();
Because I’m navigating from Child
to Parent
and finally to GrandParent
, you would think a simple join between the three entities would be used. It’s not and instead the produced query fails.
As a work around, removing FullAuditedEntity
from the Parent
class solves the issue.
Abp v3.2.4 .Net Framework v4.6.2 mySql.Data.Entity v6.9.11
EDIT:
Here’s the query generated when FullAuditedEntity is on:
SELECT `Apply1`.`Id`
,`Apply1`.`Name`
,`Apply1`.`ParentId`
FROM (
SELECT `Extent1`.`Id`
,`Extent1`.`Name`
,`Extent1`.`ParentId`
,(
SELECT `Extent2`.`Id`
FROM `GrandParents` AS `Extent2`
INNER JOIN (
SELECT `Extent3`.`Id`
,`Extent3`.`Name`
,`Extent3`.`GrandParentId`
,`Extent3`.`IsDeleted`
,`Extent3`.`DeleterUserId`
,`Extent3`.`DeletionTime`
,`Extent3`.`LastModificationTime`
,`Extent3`.`LastModifierUserId`
,`Extent3`.`CreationTime`
,`Extent3`.`CreatorUserId`
FROM `Parents` AS `Extent3`
WHERE ((`Extent3`.`IsDeleted` = 0))
AND (`Extent1`.`ParentId` = `Extent3`.`Id`) LIMIT 1
) AS `Limit1` ON `Limit1`.`GrandParentId` = `Extent2`.`Id`
WHERE (`Extent2`.`IsDeleted` = 0) LIMIT 1
) AS `ID1`
,(
SELECT `Extent2`.`Name`
FROM `GrandParents` AS `Extent2`
INNER JOIN (
SELECT `Extent3`.`Id`
,`Extent3`.`Name`
,`Extent3`.`GrandParentId`
,`Extent3`.`IsDeleted`
,`Extent3`.`DeleterUserId`
,`Extent3`.`DeletionTime`
,`Extent3`.`LastModificationTime`
,`Extent3`.`LastModifierUserId`
,`Extent3`.`CreationTime`
,`Extent3`.`CreatorUserId`
FROM `Parents` AS `Extent3`
WHERE ((`Extent3`.`IsDeleted` = 0))
AND (`Extent1`.`ParentId` = `Extent3`.`Id`) LIMIT 1
) AS `Limit1` ON `Limit1`.`GrandParentId` = `Extent2`.`Id`
WHERE (`Extent2`.`IsDeleted` = 0) LIMIT 1
) AS `NAME1`
,(
SELECT `Extent2`.`IsDeleted`
FROM `GrandParents` AS `Extent2`
INNER JOIN (
SELECT `Extent3`.`Id`
,`Extent3`.`Name`
,`Extent3`.`GrandParentId`
,`Extent3`.`IsDeleted`
,`Extent3`.`DeleterUserId`
,`Extent3`.`DeletionTime`
,`Extent3`.`LastModificationTime`
,`Extent3`.`LastModifierUserId`
,`Extent3`.`CreationTime`
,`Extent3`.`CreatorUserId`
FROM `Parents` AS `Extent3`
WHERE ((`Extent3`.`IsDeleted` = 0))
AND (`Extent1`.`ParentId` = `Extent3`.`Id`) LIMIT 1
) AS `Limit1` ON `Limit1`.`GrandParentId` = `Extent2`.`Id`
WHERE (`Extent2`.`IsDeleted` = 0) LIMIT 1
) AS `IsDeleted`
,(
SELECT `Limit1`.`GrandParentId`
FROM `GrandParents` AS `Extent2`
INNER JOIN (
SELECT `Extent3`.`Id`
,`Extent3`.`Name`
,`Extent3`.`GrandParentId`
,`Extent3`.`IsDeleted`
,`Extent3`.`DeleterUserId`
,`Extent3`.`DeletionTime`
,`Extent3`.`LastModificationTime`
,`Extent3`.`LastModifierUserId`
,`Extent3`.`CreationTime`
,`Extent3`.`CreatorUserId`
FROM `Parents` AS `Extent3`
WHERE ((`Extent3`.`IsDeleted` = 0))
AND (`Extent1`.`ParentId` = `Extent3`.`Id`) LIMIT 1
) AS `Limit1` ON `Limit1`.`GrandParentId` = `Extent2`.`Id`
WHERE (`Extent2`.`IsDeleted` = 0) LIMIT 1
) AS `GrandParentId`
FROM `Children` AS `Extent1`
) AS `Apply1`
WHERE 'My Grandparent' = `Apply1`.`NAME1`
And here’s the query generated after removing the FullAuditedEntity:
SELECT `Extent1`.`Id`
,`Extent1`.`Name`
,`Extent1`.`ParentId`
FROM `Children` AS `Extent1`
INNER JOIN `Parents` AS `Extent2` ON `Extent1`.`ParentId` = `Extent2`.`Id`
INNER JOIN `GrandParents` AS `Extent3` ON `Extent2`.`GrandParentId` = `Extent3`.`Id`
WHERE 'My Grandparent' = `Extent3`.`Name`
Issue Analytics
- State:
- Created 6 years ago
- Comments:16 (8 by maintainers)
Top GitHub Comments
@thibnes could you open a new issue and stating the problems, Abp version, Ef6/EfCore version, etc…
Hi @ismcagdas I tried to remove the SoftDelete filter as you suggested however it leads me to a System.ApplicationException: ‘Filter name SoftDelete not found’ It looks like the filter is still registered. Is there a way to bypass that?