question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CTE involving temporal table does not translate the temporal table SQL keywords

See original GitHub issue

this…

var cte = (
	from change in changeSet.TemporalAll()
	where change.Id == key
	select new
	{
		Change = change.Id,
		RowNumber = Sql.Ext.RowNumber()
			.Over()
			.PartitionBy(change.Id)
			.OrderByDesc(EF.Property<DateTime>(change, "PeriodStart"))
			.ToValue()
	})
	.AsCte("History")
	.Where(anon=>anon.RowNumber <=2)
	.ToLinqToDB();

…translates to this…

WITH [History] ([RowNumber], [Id])
AS
(
        SELECT
                ROW_NUMBER() OVER(PARTITION BY [change_1].[Id] ORDER BY [change_1].[PeriodStart] DESC),
                [change_1].[Id]
        FROM
                [Ephron.Metadata.DotNet].[Class] [change_1]
        WHERE
                [change_1].[Id] = @key_1
)
SELECT
        [anon].[Id],
        [anon].[RowNumber]
FROM
        [History] [anon]
WHERE
        [anon].[RowNumber] <= 2

… which is correct except for it is missing the temporal table keywords in the CTE…

FOR SYSTEM_TIME ALL

… it should be something like…

WITH [History] ([RowNumber], [Id])
AS
(
        SELECT
                ROW_NUMBER() OVER(PARTITION BY [change_1].[Id] ORDER BY [change_1].[PeriodStart] DESC),
                [change_1].[Id]
        FROM
                [Ephron.Metadata.DotNet].[Class] FOR SYSTEM_TIME ALL as [change_1]
        WHERE
                [change_1].[Id] = @key_1
)
SELECT
        [anon].[Id],
        [anon].[RowNumber]
FROM
        [History] [anon]
WHERE
        [anon].[RowNumber] <= 2

https://github.com/dotnet/efcore/blob/94be38cf68692d3e605175801add457936fa7331/src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs

image

I’ll attempt to implement a fix in linq2db and see if the PR will be accepted.

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Mar 22, 2023

Already released.

1reaction
sdanylivcommented, Jan 26, 2023

Will check what we can do here. Actually it needs support from linq2db side.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Temporal Table Usage Scenarios - SQL Server
Temporal Tables are useful in scenarios that require tracking history of data changes. We recommend you to consider Temporal Tables in the ...
Read more >
How to query data in a System-Versioned Temporal Tables ...
This article will cover the querying of temporal tables in SQL Server by using the FOR SYSTEM_TIME clause and its four sub clauses....
Read more >
Temporal table considerations and limitations - SQL Server
A temporal table must have a primary key defined in order to correlate records between the current table and the history table, and...
Read more >
C# SqlConnection Querying Temporal tables
I have a temporal table Employee with EmployeeHistory as its history table. In C#, I am using SqlConnection to query the data from...
Read more >
Temporal tables -- reasons not to use them?
In the project I'm working on we're about to implement 4 temporal tables in Azure SQL. In the past I've worked with version...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found