tmsmith / Dapper-Extensions

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.
1.79k stars 586 forks source link

The Predicates system did not restore the table alias #302

Open Garry-Gu opened 2 years ago

Garry-Gu commented 2 years ago

I have an interface that will query boxtask. For the first time, I will query whether there are boxes in composite status, and for the second time, I will query the number of items of a task. When I monitor the SQL generation, I find that when count is used, the alias of the table will remain at [y_1]

PredicateGroup predicateGroup = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
            predicateGroup.Predicates.Add(Predicates.Field<BoxTask>(f => f.BoxId, Operator.Eq, boxid));
            predicateGroup.Predicates.Add(Predicates.Field<BoxTask>(f => f.BoxSts, Operator.Eq, BoxStatus.stockin.ToString()));
            predicateGroup.Predicates.Add(Predicates.Field<BoxTask>(f => f.Status, Operator.Eq, TaskStatus.Wait.ToString()));
conn.Get<BoxTask>(predicateGroup)

This is the generated SQL:

SELECT [y_1].[TaskNo] AS [c_0], [y_1].[TaskSno] AS [c_1], [y_1].[BoxId] AS [c_2], [y_1].[BoxSts] AS [c_3], [y_1].[FromLoc] AS [c_4], [y_1].[ToLoc] AS [c_5], [y_1].[Priority] AS [c_6], [y_1].[Status] AS [c_7], [y_1].[IsCancel] AS [c_8], [y_1].[CrtDate] AS [c_9], [y_1].[FiniDate] AS [c_10], [y_1].[UpdDate] AS [c_11], [y_1].[Remark] AS [c_12], [y_1].[Msg] AS [c_13], [y_1].[Error] AS [c_14], [y_1].[IsPushWMS] AS [c_15] FROM [BoxTask] [y_1]
 WHERE (([y_1].[BoxId] = 'BSJ002') AND ([y_1].[BoxSts] = 'stockin') AND ([y_1].[Status] = 'Wait')) Execute time :7.2 ms,Start offset :107 ms,Errored :False

-------------------------------------------another sql

conn.Count<BoxTask>(Predicates.Field<BoxTask>(f => f.TaskNo, Operator.Eq, id))

This is the generated SQL:

SELECT COUNT(*) AS [Total] FROM [BoxTask]
 WHERE ([y_1].[TaskNo] = '57936d09-d933-49c6-9f20-89b4cfed394a') Execute time :6.4 ms,Start offset :1.8 ms,Errored :True

Cause error: Cannot bind the identifier "y_1. Taskno" composed of multiple parts.

DapperExtensions:V1.7.0

thefat32 commented 1 year ago

Having same problem. It appears to happen randomly, once it started to happen it will continue happening in every query :/

SQL generated correctly

SELECT COUNT(*) AS "Total" FROM "travel"
WHERE (("travel"."domain" = $1) AND ("travel"."status" IN ($2, $3, $4)))

SQL generated with error:

SELECT COUNT(*) AS "Total" FROM "travel"
WHERE (("y_1"."domain" = $1) AND ("y_1"."status" IN ($2, $3, $4)))