dotnet / dotnet/EntityFramework.Docs

How to query the actual value in Entity Framework core when using a Value Converter?

Open
#3,834 3 comments 1 reaction 0 assignees View on GitHub
area-change-tracking
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

## Ask a question

We have a fairly simple situation where an EF Core entity contains a value object that we map to a string:

```
builder.Property(_ => _.Nummer)
.HasConversion(i => i.ToString(), i => ZijdeNummer.FromString(i))
.IsRequired().HasMaxLength(9);
```

This works fine for retrieval and storage, but for filtering we're looking for a way to filter rows by substring. If it'd be a normal `string` column you can simply do something like this:

```
dbSet.TheTable.Where(t => t.Nummer.Contains("some text")).ToList()
```

Obviously the custom `ZijdeNummer` doesn't contain a recognized `.Contains()` method. I've tried using `.ToString().Contains()` but alas, this also doesn't work.

Finally, I've also tried accessing the column as if it were a shadow-property:

```
dbSet.TheTable.Where(t => EF.Property(t, "Nummer").Contains("some text"))
```

But it won't be fooled as it still knows that `EF.Property(t, "Nummer")` is not actually a string :(

Lastly I've dabbled in using a combination of `HasDbFunction()` with `HasTranslation()` but I couldn't get this to work, and also seems like a very difficult way to perform something that in its core seems so simple.

Is there a way to make EF-Core just query the raw column type? Any help would be greatly appreciated.

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.