'Blazor Server Custom ValidationAttribute using Database

I'm trying to implement a custom validation attribute for unique records. For that I need to check the database, so I can notify user that given data already exists.

For that I need to access database inside of IsValid method of my Custom validation attribue:

public class CustomValidationAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            //This is always null
            var context = (DataContext)validationContext.GetService(typeof(DataContext));
            
        }
    }

I alredy tried to implement custom DataAnotation with DI acording to this post: stack overflow link

but it does not work. Maybe due to .net6 changes? I get error that EditContext cannot by changed dynamicaly.

Can some point me in the rigth direction please? Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source