I need to manage hierarchy data storing in my database. But now I am facing a problem. I am using entity sql for my asp.net. So now, how can I convert those sql to entity query? Here is the SQL query I suppose converts to entity framework query
WITH RPL ( P_TASK_ID, C_TASK_ID,taskSeq) AS
( SELECT root.P_TASK_ID, root.C_TASK_ID ,root.Seq
FROM [COMMON.TASK_REL_test] as root
UNION ALL
SELECT child.P_TASK_ID, child.C_TASK_ID, child.Seq
FROM [COMMON.TASK_REL_test] parent, [COMMON.TASK_REL_test] child
WHERE parent.C_TASK_ID = CHILD.P_TASK_ID
)
SELECT DISTINCT P_TASK_ID, C_TASK_ID,taskSeq
FROM RPL
ORDER BY P_TASK_ID, C_TASK_ID,taskSeq;
Here is my table structure
pID CID Seq
NULL 1 1
1 2 1
1 3 2
1 4 3
2 5 1
2 6 2
3 7 1
Here is my insert table query
INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID, Seq)
values (null,1,1)
INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (1,2,1)
INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (1,4,3)
INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (1,3,2)
INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (2,6,2)
INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (2,5,1)
INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (3,7,1)
Here is my coding
private void createGridView()
{
try
{
using (ObjectContext ctx = new ObjectContext(gbcDbConnection.eObjqueryConnection))
{
string result = @"
;WITH RPL ( P_TASK_ID, C_TASK_ID,taskSeq) AS
( SELECT root.P_TASK_ID, root.C_TASK_ID ,root.Seq
FROM LEWREDBEntities.[COMMON_TASK_REL_test] as root
UNION ALL
SELECT child.P_TASK_ID, child.C_TASK_ID, child.Seq
FROM LEWREDBEntities.[COMMON_TASK_REL_test] as parent, LEWREDBEntities.[COMMON_TASK_REL_test] as child
WHERE parent.C_TASK_ID = CHILD.P_TASK_ID
)
SELECT DISTINCT P_TASK_ID, C_TASK_ID,taskSeq
FROM RPL
ORDER BY P_TASK_ID, C_TASK_ID,taskSeq";
ObjectQuery<DbDataRecord> query = ctx.CreateQuery<DbDataRecord>(result);
string cde = query.ToTraceString();
}
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
But I am getting an error now.
The query syntax is not valid. Near term ';', line 2, column 34..
Please help.