Efcore: Trimming trailing spaces on char type

Created on 31 Jan 2018  路  2Comments  路  Source: dotnet/efcore

Is there any sort of hook or interception where a char column can be trimmed so that the trailing spaces are removed?

I'm looking for a solution similar to this which is available in EF 6.1

Thanks

closed-duplicate

Most helpful comment

@amcorch In EF Core 2.1 a value converter can do this. Something like:
C# modelBuilder .Entity<Foo>() .Property(e => e.Bar) .HasConversion(v => v.Trim(), v => v.Trim());
This will remove any whitespace when coming out of or going into the database.

In EF Core 2.0 I don't think that there is an easy way to trim when data is coming from the database. You could override SaveChanges to trim going into the database.

All 2 comments

@amcorch In EF Core 2.1 a value converter can do this. Something like:
C# modelBuilder .Entity<Foo>() .Property(e => e.Bar) .HasConversion(v => v.Trim(), v => v.Trim());
This will remove any whitespace when coming out of or going into the database.

In EF Core 2.0 I don't think that there is an easy way to trim when data is coming from the database. You could override SaveChanges to trim going into the database.

Closing as duplicate of #242

Was this page helpful?
0 / 5 - 0 ratings