You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Looks legit, but actually there's an error - the key is composite, so it must be .HasKey(p => new { p.UserId, p.Timestamp}). The thing is that I didn't notice this for the first time. So, I get the following results:
H-m-m... Same data and time. Let's take a look at generated SQL in profiler:
SELECT [l].[user_id], [l].[timestamp]
FROM [dbo].[logger] AS [l]
The generated SQL is as expected.
So, why EF didn't throw exception and let bogus data to return?
Include provider and version information
EF Core version: 6.0.0-preview.1.21102.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer (SQL Server 2017 Microsoft SQL Server 2017 14.0.3381.3 x64)
Target framework: .NET 6.0.100-preview.1.21103.13
Operating system: Windows 10 Home x64 build 19042
IDE: Visual Studio 2019 16.9.0 Preview 5.0
The text was updated successfully, but these errors were encountered:
JohnyL
changed the title
EF doesn't generate error and return incorrect result when using only part of composite key
EF doesn't throw exception and returns incorrect result when using only part of composite key
Feb 28, 2021
@JohnyL This is by-design. To avoid performing redundant checks EF assumes that the model matches the database. #831 should help to catch these types of mistakes.
If EF "assumes", then why dealing with exceptions? You could equally return bogus data in all cases where exceptions should be. I was figuring out the whole day this problem.
File a bug
Say, I have a simple table with two columns:
Next, setup database context:
Looks legit, but actually there's an error - the key is composite, so it must be
.HasKey(p => new { p.UserId, p.Timestamp})
. The thing is that I didn't notice this for the first time. So, I get the following results:H-m-m... Same data and time. Let's take a look at generated SQL in profiler:
The generated SQL is as expected.
So, why EF didn't throw exception and let bogus data to return?
Include provider and version information
EF Core version: 6.0.0-preview.1.21102.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer (SQL Server 2017 Microsoft SQL Server 2017 14.0.3381.3 x64)
Target framework: .NET 6.0.100-preview.1.21103.13
Operating system: Windows 10 Home x64 build 19042
IDE: Visual Studio 2019 16.9.0 Preview 5.0
The text was updated successfully, but these errors were encountered: