Hello!
I ran into a problem with LINQ where the generated SQL seems to be wrong.
I saw another post (http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=13735&HighLight=1) with a similar problem but the proposed solution there didn't make it for me.
My data model is (see attached jpg):
--------- 1 * ------------------- * 1 ------------
| Video | ------->| VideoCategory |<---------| Category |
--------- ------------------- -------------
Video has many Categories and is connected through an intermediary table VideoCategory.
I want to get all the Categories that are NOT connected to a specific Video. I try to run the following LINQ:
var allCat = from c1 in linqMetaData.Category
select c1;
var videoCat = from c in linqMetaData.Category
join vc in linqMetaData.VideoCategory on c.CategoryId equals vc.CategoryId
join v in linqMetaData.Video on vc.VideoId equals v.VideoId
where v.VideoId == 3
select c;
var diff = allCat.Except(videoCat);
The above code is valid LINQ and I am able to retrieve the correct data from the database if I'm using e.g. LinqPad to execute LINQ directly in the database. However, when I try to run this through LLBLGen I get the following exception:
SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException was unhandled
Message="An exception was caught during the execution of a retrieval query: The multi-part identifier \"LPLA_2.CategoryId\" could not be bound.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception."
The generated SQL:
SELECT DISTINCT [LPLA_1].[CategoryId], [LPLA_1].[Name], [LPLA_1].[Description] FROM [WeBeFree].[dbo].[Category] [LPLA_1] WHERE ( ( ( NOT ( EXISTS (SELECT [LPA_L1].[CategoryId] FROM (SELECT [LPA_L2].[CategoryId], [LPA_L2].[Name], [LPA_L2].[Description] FROM (( [WeBeFree].[dbo].[Category] [LPA_L2] INNER JOIN [WeBeFree].[dbo].[VideoCategory] [LPA_L3] ON [LPA_L2].[CategoryId] = [LPA_L3].[CategoryId]) INNER JOIN [WeBeFree].[dbo].[Video] [LPA_L4] ON [LPA_L3].[VideoId] = [LPA_L4].[VideoId]) WHERE ( ( ( [LPA_L4].[VideoId] = 3)))) [LPA_L1] WHERE ( [LPLA_2].[CategoryId] = [LPA_L1].[CategoryId]))))))
The generated SQL contains an error. At the very end if I change
[LPLA_2].[CategoryId]
to be
[LPLA_1].[CategoryId]
everything works and I get the correct result.
I downloaded the latest build today July 7th and regenerated everything but it doesn't help. Any ideas, work arounds or fixes?
Thank you for your time.