Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
hexagonite authored Sep 10, 2023
1 parent 6c45855 commit f708392
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ public class UlidToBytesConverter : ValueConverter<Ulid, byte[]>
{
private static readonly ConverterMappingHints defaultHints = new ConverterMappingHints(size: 16);

public UlidToBytesConverter() : this(null)
{
}

public UlidToBytesConverter(ConverterMappingHints mappingHints = null)
: base(
convertToProviderExpression: x => x.ToByteArray(),
Expand All @@ -247,6 +251,10 @@ public class UlidToStringConverter : ValueConverter<Ulid, string>
{
private static readonly ConverterMappingHints defaultHints = new ConverterMappingHints(size: 26);

public UlidToStringConverter() : this(null)
{
}

public UlidToStringConverter(ConverterMappingHints mappingHints = null)
: base(
convertToProviderExpression: x => x.ToString(),
Expand All @@ -257,6 +265,29 @@ public class UlidToStringConverter : ValueConverter<Ulid, string>
}
```

To use those converters, you can either specify individual properties of entities in `OnModelCreating` method of your context:
```csharp
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>()
.Property(e => e.MyProperty)
.HasConversion<UlidToStringConverter>()
.HasConversion<UlidToBytesConverter>();
}
```

or use model bulk configuration for all properties of type `Ulid`. To do this, overload `ConfigureConventions` method of your context:

```csharp
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder
.Properties<Ulid>()
.HaveConversion<UlidToStringConverter>()
.HaveConversion<UlidToBytesConverter>();
}
```

License
---
This library is under the MIT License.

0 comments on commit f708392

Please sign in to comment.