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