Mojtaba Khooryani
Mojtaba Khooryani

Reputation: 1875

EF Core Mark a property in configuration and check if that property contains the mark in UoW

Does EF Core support this feature?

I need to mark properties in configuration and check if that property contains the specified mark in UoW using the change tracker.

for example something like this:

builder.Property(x => x.Id)
            .DisableAudit();

and use it in UoW:

        var auditables = _context
            .ChangeTracker
            .Entries<IAuditable>()
            .ToList();

        foreach (var entity in auditables)
        {
            foreach (var property in entity.Properties)
            {
                if (AuditIsDisabled(property))
                {
                    // ...
                }

Upvotes: 0

Views: 398

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27416

It can be achieved by annotations:

public static class AuditExtensions
{
    public const string CustomDisableAudit = "custom:disable_audit";

    public static PropertyBuilder<TProperty> DisableAudit<TProperty>(this PropertyBuilder<TProperty> property)
    {
        return property.HasAnnotation(CustomDisableAudit, true);
    }

    public static bool IsAuditDisabled(this PropertyEntry propertyEntry)
    {
        return propertyEntry.Metadata.IsAuditDisabled();
    }

    public static bool IsAuditDisabled(this IProperty property)
    {
        return property.FindAnnotation(CustomDisableAudit)?.Value as bool? == true;
    }
}

Upvotes: 1

Related Questions