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.

Query of the history of a temporary table returns additional records

See original GitHub issue

File a bug

Imagine that you have a simple invoices and InvoicesHistory entities, also imagine that you have already inserted some records (only inserted, not updated ).

So if you query from SQL Server Management Studio (SSMS), the [InvoicesHistory] table has no records. (Of course, because there were no updates)

The surprise is that if you query from EFC , as many records as the temporary table has are retrieved. i.e

Query from SSMS Invoices has 5 insertions, InvoicesHistory has 0 insertions (because Invoices were no updates)

The surprise is that if you query the InvoicesHistory table from EFC, it returns 5 records. (for this example)

 var invoicesHistory = context.Invoices.TemporalAll().ToList();  // invoicesHistory.Count =5
 var count = context.Invoices.TemporalAll().ToList().Count;   // count = 5 
     /*
     * If the records were not updated, the historical table should not retrieve  records,
     * you would expect the same behavior as SQL Server. 
     */

stack traces

   at System.Environment.get_StackTrace()\r\n   at MyForms.Form1.button1_Click(Object sender, EventArgs e) in C:\\Users\\PHILIPS.JESUSMENDOZA\\source\\repos\\MyForms\\Form1.cs:line 71\r\n   at System.Windows.Forms.Control.OnClick(EventArgs e)\r\n   at System.Windows.Forms.Button.OnClick(EventArgs e)\r\n   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)\r\n   at Interop.User32.DispatchMessageW(MSG& msg)\r\n   at Interop.User32.DispatchMessageW(MSG& msg)\r\n   at System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(UIntPtr dwComponentID, msoloop uReason, Void* pvLoopData)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(msoloop reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(msoloop reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.Run(Form mainForm)\r\n   at MyForms.Program.Main() in C:\\Users\\PHILIPS.JESUSMENDOZA\\source\\repos\\MyForms\\Program.cs:line 14

Include provider and version information

EF Core version: EFC 6.0.1 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 Operating system: Edición Windows 10 Pro Versión 21H2 Se instaló el ‎08/‎12/‎2021 Compilación del SO 19044.1387 Experiencia Windows Feature Experience Pack 120.2212.3920.0

IDE: Microsoft Visual Studio Professional 2022 (64-bit) Version 17.0.3

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
maumarcommented, Dec 19, 2021

TemporalAll maps to FOR SYSTEM_TIME ALL operation on sql server, which according to tsql documentation “Returns the union of rows that belong to the current and the history table.”. So we should adjust documentation to reflect that. But should consider providing a (convenient) way to query the history table directly given the discrepancy between ALL and history table shown in this scenario. For now you can use FromSql - make sure to use NoTracking

0reactions
ajcvickerscommented, Jan 3, 2022

@mendozagit

1-Could use normal properties (not shadow properties).

This is tracked by #26463.

2-Could the properties PeriodEnd and PeriodStart be nullable (for semantics) 3-There could be a simple and transparent way to query historical data,

The EF Core implementation aligns with the way this is implement in SQL Server. See https://docs.microsoft.com/en-us/sql/relational-databases/tables/temporal-tables?view=sql-server-ver15. The way the columns and queries are handled matches how temporal tables are implemented by SQL Server. I don’t think it makes sense for EF to do something different here; that would both be confusing, and would create mapping issues when attempting to go from EF to SQL Server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to track the history of data changes using SQL Server ...
In this blog post, you are going to see how to track the history of data changes using SQL Server 2016 System-Versioned Temporal...
Read more >
Query data in a system-versioned temporal table
When you want to get latest (current) state of data in a temporal table, you can query the same way as you query...
Read more >
if multiple users run a query that creates a temp table at ...
I am using postgres12. I have a query that creates a temp table named table_x and returns it. I am wondering if this...
Read more >
Temporary table record limit in Sql server
Temporary tables are stored in the tempdb Database, and can grow as long as tempdb itself can grow (i.e. until the disk is...
Read more >
Temporary Tables in SQL Server - Simple Talk
Anyone who has access to TempDB at the time these Global Temporary tables exist can directly query, modify or drop these temporary objects....
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