| | 1 | | using System; |
| | 2 | | using Jellyfin.Database.Providers.Sqlite.ValueConverters; |
| | 3 | | using Microsoft.EntityFrameworkCore; |
| | 4 | | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
| | 5 | |
|
| | 6 | | namespace Jellyfin.Database.Providers.Sqlite; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Model builder extensions. |
| | 10 | | /// </summary> |
| | 11 | | public static class ModelBuilderExtensions |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Specify value converter for the object type. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="modelBuilder">The model builder.</param> |
| | 17 | | /// <param name="converter">The <see cref="ValueConverter{TModel,TProvider}"/>.</param> |
| | 18 | | /// <typeparam name="T">The type to convert.</typeparam> |
| | 19 | | /// <returns>The modified <see cref="ModelBuilder"/>.</returns> |
| | 20 | | public static ModelBuilder UseValueConverterForType<T>(this ModelBuilder modelBuilder, ValueConverter converter) |
| | 21 | | { |
| 4 | 22 | | var type = typeof(T); |
| 240 | 23 | | foreach (var entityType in modelBuilder.Model.GetEntityTypes()) |
| | 24 | | { |
| 2656 | 25 | | foreach (var property in entityType.GetProperties()) |
| | 26 | | { |
| 1212 | 27 | | if (property.ClrType == type) |
| | 28 | | { |
| 40 | 29 | | property.SetValueConverter(converter); |
| | 30 | | } |
| | 31 | | } |
| | 32 | | } |
| | 33 | |
|
| 4 | 34 | | return modelBuilder; |
| | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Specify the default <see cref="DateTimeKind"/>. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="modelBuilder">The model builder to extend.</param> |
| | 41 | | /// <param name="kind">The <see cref="DateTimeKind"/> to specify.</param> |
| | 42 | | public static void SetDefaultDateTimeKind(this ModelBuilder modelBuilder, DateTimeKind kind) |
| | 43 | | { |
| 2 | 44 | | modelBuilder.UseValueConverterForType<DateTime>(new DateTimeKindValueConverter(kind)); |
| 2 | 45 | | modelBuilder.UseValueConverterForType<DateTime?>(new DateTimeKindValueConverter(kind)); |
| 2 | 46 | | } |
| | 47 | | } |