This is using LLBLGen 3.5, Self Servicing. I downloaded the latest version today.
QuerySpec.dll version: 3.5.12.0522
ORMSupporterClasses.NET20.dll version: 3.5.12.0507
I have the following code:
qf = new QueryFactory();
new TypedListDAO().FetchQuery(
qf.Create()
.From(qf.Supporter.InnerJoin(qf.Project).On(SupporterFields.ClientId == ProjectFields.ClientId))
.Where(SupporterFields.SupporterId == 3)
.Select(() => new { ProjectId = ProjectFields.ProjectId.ToValue<int>() })
);
The "Project" entity is at the root of a TargetPerEntity hierarchy.
The SQL generated is this:
SELECT [myDB].[dbo].[ap_Project].[ProjectId]
FROM ( [myDB].[dbo].[ap_Supporter]
INNER JOIN [myDB].[dbo].[ap_Project] [LPA_L1]
ON [myDB].[dbo].[ap_Supporter].[ClientId] = [myDB].[dbo].[ap_Project].[ClientId])
WHERE ((([myDB].[dbo].[ap_Supporter].[SupporterId] = @p1)))
The Error is:
Message: The multi-part identifier "myDB.dbo.ap_Project.ClientId" could not be bound.
Note that in the JOIN, the ap_Project table has been given the alias of "LPA_L1", but that alias was not used in the select, which causes the error.
I can work around this by specifying an alias manually (same code, just use alias of "P"):
qf = new QueryFactory();
new TypedListDAO().FetchQuery(
qf.Create()
.From(qf.Supporter.InnerJoin(qf.Project.As("P")).On(SupporterFields.ClientId == ProjectFields.ClientId.Source("P")))
.Where(SupporterFields.SupporterId == 2)
.Select(() => new { ProjectId = ProjectFields.ProjectId.Source("P").ToValue<int>() })
);
Generates proper SQL:
SELECT [LPA_P1].[ProjectId]
FROM ( [myDB].[dbo].[ap_Supporter]
INNER JOIN [myDB].[dbo].[ap_Project] [LPA_P1]
ON [myDB].[dbo].[ap_Supporter].[ClientId] = [LPA_P1].[ClientId])
WHERE ((([myDB].[dbo].[ap_Supporter].[SupporterId] = @p1)))
Similar code where I join an entity that is not part of a TargetPerEntity hierarchy does not show the problem (join to Name instead of Project):
qf = new QueryFactory();
new TypedListDAO().FetchQuery(
qf.Create()
.From(qf.Supporter.InnerJoin(qf.Name).On(SupporterFields.NameId == NameFields.NameId))
.Where(SupporterFields.SupporterId == 1)
.Select(() => new { NameId = NameFields.NameId.ToValue<int>() })
);
SQL:
SELECT [myDB].[dbo].[ap_Name].[NameId]
FROM ( [myDB].[dbo].[ap_Supporter]
INNER JOIN [myDB].[dbo].[ap_Name]
ON [myDB].[dbo].[ap_Supporter].[NameId] = [myDB].[dbo].[ap_Name].[NameId])
WHERE (((myDB].[dbo].[ap_Supporter].[SupporterId] = @p1)))
For this reason, I'm thinking that this may have to do with the fact that ap_Project is part of a TargetPerEntity hierarchy, but that's just a guess - it might have to do with something else. And I'm seeing this with QuerySpec code - not sure if that's a factor or not.
Thanks,
Wes