/
EntityBaseExtensions.cs
58 lines (54 loc) · 2.58 KB
/
EntityBaseExtensions.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
49
50
51
52
53
54
55
56
57
58
// MADE Apps licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace MADE.Data.EFCore.Extensions
{
using System;
using MADE.Data.EFCore.Converters;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
/// <summary>
/// Defines a collection of extensions for the <see cref="EntityBase"/> type.
/// </summary>
public static class EntityBaseExtensions
{
/// <summary>
/// Configures the default properties of an <typeparamref name="TEntity">entity</typeparamref>.
/// </summary>
/// <typeparam name="TEntity">The type of entity to configure.</typeparam>
/// <param name="builder">The entity type builder associated with the entity.</param>
/// <returns>The entity type builder.</returns>
public static EntityTypeBuilder<TEntity> Configure<TEntity>(this EntityTypeBuilder<TEntity> builder)
where TEntity : class, IEntityBase
{
builder.ConfigureWithKey<TEntity, Guid>();
return builder;
}
/// <summary>
/// Configures the default properties of an <typeparamref name="TEntity">entity</typeparamref>.
/// </summary>
/// <typeparam name="TEntity">The type of entity to configure.</typeparam>
/// <typeparam name="TKey">The type of unique identifier for the entity.</typeparam>
/// <param name="builder">The entity type builder associated with the entity.</param>
/// <returns>The entity type builder.</returns>
public static EntityTypeBuilder<TEntity> ConfigureWithKey<TEntity, TKey>(this EntityTypeBuilder<TEntity> builder)
where TEntity : class, IEntityBase<TKey>
{
builder.HasKey(e => e.Id);
builder.ConfigureDateProperties();
return builder;
}
/// <summary>
/// Configures the created and updated date properties of an <typeparamref name="TEntity">entity</typeparamref> as UTC.
/// </summary>
/// <typeparam name="TEntity">The type of entity to configure.</typeparam>
/// <param name="builder">The entity type builder associated with the entity.</param>
/// <returns>The entity type builder.</returns>
public static EntityTypeBuilder<TEntity> ConfigureDateProperties<TEntity>(
this EntityTypeBuilder<TEntity> builder)
where TEntity : class, IDatedEntity
{
builder.Property(x => x.CreatedDate).IsUtc();
builder.Property(x => x.UpdatedDate).IsUtc();
return builder;
}
}
}