Repository navigation
Description
There are significant problems when executing queries that either convert nulls in the database to non-nulls in code or vice-versa. Therefore, we have marked this feature as internal for EF Core 6.0. You can still use it, but you will get a compiler warning. The warning can be disabled with a #pragma. See #26230 for more information.
When using FromSql to create a custom filtered data set, any column conversions defined on the DbQuery object don't get applied.
I think the column conversions should be included automatically around the sub-query supplied to FromSql, similar to how a sub-query is used if more predicates are added to the IQueryable.
public class Model
{
public int Id { get; set; }
public bool Flag { get; set; }
}
public partial class MyDbContext : DbContext
{
public DbQuery<Model> Models { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Query<Models>(
entity =>
{
entity.ToView("dbo.Models");
// Convert null Flag to false on way in
entity.Property(e => e.Flag).HasConversion<bool?>(f => f, t => t ?? false);
});
}
}
// ...
var models = dbContext.Models.FromSql("SELECT * FROM dbo.Models").ToList();This, I think, should result in SQL similar to this being executed...
SELECT [e].[Id], COALESCE([e].[Flag], 0) AS [Flag]
FROM (
SELECT * FROM dbo.Models) AS [e];But, in reality, the plain SQL from FromSql is executed
SELECT * FROM dbo.ModelsEF Core version: 2.1.4
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 7
IDE: Visual Studio 2017 15.8.5
Activity
ajcvickers commentedon Nov 1, 2018
@MarkGodwin Value conversions are not translated to SQL--that feature is tracked by #10861. Instead, the value is read from the database and then converted. This is because the primary purpose of this feature it to allow use of types that cannot be directly read from or written to the database.
Also, value converters do not generally allow the conversion of null to some other value. This is because the same value converter can be used for both nullable and non-nullable types, which is very useful for PK/FK combinations where the FK is often nullable and the PK is not. We will discuss whether or not to add support for converting nulls.
MarkGodwin commentedon Nov 1, 2018
Sorry, I can see that I am guilty of making assumptions that I didn't verify before raising the issue. I misinterpreted the documentation about where value converters are executed, and then jumped to conclusions about why it wasn't working in my particular case.
The inability to translate null is also very clearly described in the documentation.
I... assume... a correctly implemented value converter would be executed when mapping the results of a
FromSql. So I'll close this off immediately.Apologies again.
[-]HasConversion property configuration not applied to FromQuery results[/-][+]Allow HasConversion/ValueConverters to convert nulls[/+]space-alien commentedon Mar 12, 2019
The inability to translate nulls leads to some pretty unintuitive behaviour:
The above is mapped to a nullable string column in the DB. If Address is null, a NULL is inserted into the row and vice versa. So far so good.
But the results of following query include entities where Address is null!
Skulblaka commentedon Apr 8, 2019
I ran into a problem when I tried to add a
ValueConverter<float, float?>that convertsfloat.NaNtonulland vice versa since SQL Server'srealdata type does not supportNaN. The generated columns for the properties with the converter did not allownullvalues; I guess this is related?ajcvickers commentedon Apr 8, 2019
@Skulblaka Yes.
UdiAzulay commentedon Oct 13, 2019
any success to use conversion from non null to nullable type? (without excluding the property using NonMapped)
UdiAzulay commentedon Oct 13, 2019
found an ugly solution using ValueConverter<T, T?>(optionally) and the below reflection field update
prop.Metadata.GetType().GetField("_isNullable", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(prop.Metadata, true);
52 remaining items
ajcvickers commentedon Nov 19, 2024
Notes from team meeting: I was thinking about this again following investigation of #35051 and I think the cases where nulls have special behavior--for example, when used in a PK or FK column/property--can be detected in model validation. This will allow us to mark this code as public to support the cases that work well, while still generating a warning (possibly as an error by default) for cases where the behavior may be unexpected.
AndriySvyryd commentedon Nov 19, 2024
Also, for columns of optional dependents using table sharing.
ivebeenlinuxed commentedon May 29, 2025
I had some success with adding
ConvertsNulls... I'm writing this a little confused, because if it it really was that simple, surely someone else would be doing it and this topic wouldn't be here....