weitzhandler / FluentValidation.EntityFrameworkCore

Loads EF Core column configuration from matching FluentValidation validators registered in assembly.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FluentValidation.EntityFrameworkCore

Loads EF Core column configuration from matching FluentValidation validators registered in assembly.

Usage:

Install the Weitzhandler.FluentValidation.EntityFrameworkCore package, and set up FluentValidation.DependencyInjection to register all validators:

services
  .AddDbContext<EntityContext>(options => ...)
  .AddValidatorsFromAssemblyContaining<EntityValidator>();

Then, in your DbContext's OnModelCreating method, call modelBuilder.ApplyConfigurationFromFluentValdations(serviceProvider):

public class EntityContext : DbContext
{    
    private readonly IServiceProvider serviceProvider;

    public EntityContext(DbContextOptions<EntityContext> options, IServiceProvider serviceProvider)
        : base(options)
    {
        this.serviceProvider = serviceProvider;            
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfigurationFromFluentValidations(serviceProvider);
    }
}

Alternatively, call AddFluentValidation on the IServiceCollection, optionally providing validator discovery assemblies, then have your DbContext take a dependency on IValidatorFactory:

public class EntityContext : DbContext
{    
    private readonly IValidatorFactory validatorFactory;

    public EntityContext(DbContextOptions<EntityContext> options, IValidatorFactory validatorFactory)
        : base(options)
    {
        this.validatorFactory = validatorFactory;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfigurationFromFluentValidations(validatorFactory);
    }
}

Note: we need a reference to the service provider so that we obtain the registered IValidator<>s from it.

The following validators are currently supported:

  • INotNullValidator/INotEmptyValidator (e.g. RuleFor(entity => entity.Property.NotNull()))
    Annotates the Db column as not-nullable (e.g. Column (nvarchar(MAX), not null)
  • ILengthValidator (e.g. RuleFor(entity => entity.Property.MaximumLength(m)))
    Annotates the Db column with a max length annotation (e.g. varchar(m)).
  • ExactLengthValidator
    Annotates the Db column with a fixed length annotation (e.g. varchar(f)).
  • ScalePrecisionValidator (RuleFor(entity => entity.Property.ScalePrecision(scale: s, precision: p)))
    Annotates the Db column (if decimal) with the precision and scale This is only supported in EF Core 5+

Release notes

0.3.2

Bug fix

Release notes:

0.4.0

Added .NET 6 support

About

Loads EF Core column configuration from matching FluentValidation validators registered in assembly.

License:Apache License 2.0


Languages

Language:C# 100.0%