Skip to content

Commit 0b42a72

Browse files
committed
Added base entity class and extensions for configuring entity dates as UTC
1 parent 279d758 commit 0b42a72

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/MADE.Data.EFCore/EntityBase.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace MADE.Data.EFCore
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Defines a base definition for an entity.
7+
/// </summary>
8+
public abstract class EntityBase
9+
{
10+
/// <summary>
11+
/// Gets or sets the identifier of the entity.
12+
/// </summary>
13+
public Guid Id { get; set; }
14+
15+
/// <summary>
16+
/// Gets or sets the date of the entity's creation.
17+
/// </summary>
18+
public virtual DateTime CreatedDate { get; set; } = DateTime.UtcNow;
19+
20+
/// <summary>
21+
/// Gets or sets the date of the entity's last update.
22+
/// </summary>
23+
public virtual DateTime? UpdatedDate { get; set; } = DateTime.UtcNow;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace MADE.Data.EFCore.Extensions
2+
{
3+
using MADE.Data.EFCore.Converters;
4+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
5+
6+
/// <summary>
7+
/// Defines a collection of extensions for the <see cref="EntityBase"/> type.
8+
/// </summary>
9+
public static class EntityBaseExtensions
10+
{
11+
/// <summary>
12+
/// Configures the created and updated date properties of an <typeparamref name="TEntity">entity</typeparamref> as UTC.
13+
/// </summary>
14+
/// <typeparam name="TEntity">The type of entity to configure.</typeparam>
15+
/// <param name="builder">The entity type builder associated with the entity.</param>
16+
/// <returns>The entity type builder.</returns>
17+
public static EntityTypeBuilder<TEntity> ConfigureDateProperties<TEntity>(this EntityTypeBuilder<TEntity> builder)
18+
where TEntity : EntityBase
19+
{
20+
builder.Property(x => x.CreatedDate).IsUtc();
21+
builder.Property(x => x.UpdatedDate).IsUtc();
22+
return builder;
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)