dotnet / dotnet/EntityFramework.Docs

Backing field for required bool property with default value

Open
#3,360 2 comments 0 reactions 0 assignees View on GitHub
area-save-changes
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

Hi,

I saw in [this article](https://www.learnentityframeworkcore5.com/whats-new-in-ef-core-5#new-pattern-for-store-generated-defaults) that EF Core 5 now supports nullable backing field for properties with store-generated defaults.

I found it interesting as I want to keep the entities consistent with the domain (non-nullable is required).

So I decided to try it with this configuration:
```csharp
builder.Ignore(e => e.IsValid);

builder.Property("_isValid")
.HasColumnName("IsValid")
.HasDefaultValue(false);
```

But the public property can't be used in queries.

Is there a way to make it work ?

Thanks

Edit:

I found that I can use in LINQ query:

```csharp
[...].Where(e => EF.Property(e, "_isValid"))
```

But this is really not straightforward...

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.