dotnet / dotnet/efcore

Can't compare owned JSON entity to parameterized null

Open
#36,401 0 comments 0 reactions 0 assignees View on GitHub
area-json area-owned-entities area-query
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

```c#
// The following works, with an inline null, producing WHERE [e].[OptionalRelated] IS NULL
_ = await context.Entities
.Where(e => e.OptionalRelated == null)
.ToListAsync();

// The following fails, with a parameterized null, producing WHERE 0 = 1
Related? related = null;
_ = await context.Entities
.Where(e => e.OptionalRelated == related)
.ToListAsync();
```

Repro

```c#
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

context.Entities.Add(new Entity
{
OptionalRelated = null
});
await context.SaveChangesAsync();

// The following works, with an inline null, producing WHERE [e].[OptionalRelated] IS NULL
_ = await context.Entities
.Where(e => e.OptionalRelated == null)
.ToListAsync();

// The following fails, with a parameterized null, producing WHERE 0 = 1
Related? related = null;
_ = await context.Entities
.Where(e => e.OptionalRelated == related)
.ToListAsync();

public class BlogContext : DbContext
{
public DbSet Entities { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity(b =>
{
b.OwnsOne(b => b.OptionalRelated/*, o => o.ToJson()*/);
b.Navigation(x => x.OptionalRelated).IsRequired(false);
});
}
}

public class Entity
{
public int Id { get; set; }

public Related? OptionalRelated { get; set; } = null!;
}

public class Related
{
public int Foo { get; set; }
}
```

Test: OwnedJsonStructuralEqualityRelationalTestBase.Related_with_parameter_null

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.