/// <summary> /// Defines a 'unique inside of DbSet' validator on the current rule builder. /// Validation will fail if the value of the property is not unique within the column /// of the given <paramref name="dbSet"/>. /// The column is specified by <paramref name="getColumnSelector"/>. /// </summary> /// <param name="ruleBuilder"></param> /// <param name="dbSet"></param> /// <param name="getColumnSelector"> /// Determines the column, with which we will compare the value of the property. /// </param> /// <typeparam name="TObject"> /// Type of object being validated /// </typeparam> /// <typeparam name="TProperty"> /// Type of property being validated /// </typeparam> /// <typeparam name="TEntity"> /// Type of entity, with which <paramref name="dbSet"/> operates /// </typeparam> /// <returns></returns> public static IRuleBuilderOptions <TObject, TProperty> UniqueInsideOfDbSetColumn <TObject, TProperty, TEntity>( this IRuleBuilder <TObject, TProperty> ruleBuilder, DbSet <TEntity> dbSet, Expression <Func <TEntity, TProperty> > getColumnSelector) where TObject : class where TEntity : class { return(ruleBuilder .MustAsync ( (newTitle, token) => EfCoreValidationHelpers.IsValueUniqueInsideOfDbSetColumnAsync (dbSet, getColumnSelector, newTitle, token) ) .WithMessage("{PropertyName} must be unique")); }
/// <summary> /// Checks if the given values are unique for the columns combination, /// specified by <paramref name="getColumnsCombination"/>, in <paramref name="dbSet"/>. /// </summary> /// <param name="ruleBuilder"></param> /// <param name="dbSet"></param> /// <param name="getColumnsCombination"> /// Determines the columns combination. /// By columns combination we mean object, containing multiple columns, /// <para>e.g. <c>comment => new { comment.CommentId, comment.UserId }</c>.</para> /// </param> public static IRuleBuilderOptions <TObject, object[]> UniqueForColumnsCombinationInDbSet <TObject, TColumnsCombination, TEntity> ( this IRuleBuilder <TObject, object[]> ruleBuilder, DbSet <TEntity> dbSet, Expression <Func <TEntity, TColumnsCombination> > getColumnsCombination ) where TObject : class where TEntity : class { return(ruleBuilder .MustAsync ( (values, token) => EfCoreValidationHelpers.AreValuesUniqueForColumnsCombinationInDbSetAsync (dbSet, getColumnsCombination, values, token) ) .WithMessage("The given values are not unique for the specified columns combination")); }