简体   繁体   English

在自定义验证属性中使用数据库上下文

[英]Using DB Context in a Custom Validation Attribute

I am trying to create a valdiation attribute in my Core 2 project. 我正在尝试在Core 2项目中创建一个验证属性。 It needs to validate the value against a list of existing values held in the database. 它需要对照数据库中保存的现有值列表来验证该值。

The code below isn't working, it isn't able to access the DB Context. 下面的代码不起作用,它无法访问数据库上下文。

Any ideas why/how to correct? 任何想法为什么/如何纠正?

public class BibValidatorAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(
        object value, ValidationContext validationContext)
    {
        RaceEntryViewModel raceEntry = (RaceEntryViewModel)validationContext.ObjectInstance;
        ApplicationDbContext _context = new ApplicationDbContext();

        var dbraceEntry = _context.RaceEntries.FirstOrDefault(c => c.Id == raceEntry.Id);

        if(raceEntry.BibNumber != dbraceEntry.BibNumber)
        {
            if (value != null)
            {
                var raceentries = from r in _context.RaceEntries
                                  select r;

                var mycount = raceentries.Count(c => c.BibNumber == raceEntry.BibNumber);

                if (mycount != 0)
                {
                    return new ValidationResult("The bib number entered already exists");
                }
                else
                {
                    return ValidationResult.Success;
                }
            }
            else
            {
                return ValidationResult.Success;
            }
        }
        else
        {
            return ValidationResult.Success;
        }        
    }
}

What i found you can do is retrieve the DB Context from the ValidationContext which I didn't realize you could do using GetService. 我发现您可以做的是从ValidationContext中检索数据库上下文,但我没有意识到您可以使用GetService完成。

var _context = (ApplicationDbContext)validationContext
                         .GetService(typeof(ApplicationDbContext));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用反射的自定义验证属性? - Custom Validation Attribute using Reflection? 使用C#和Web API并使用带有验证上下文的私有访问修饰符的自定义必需属性 - Custom required attribute with C# & Web API and using private access modifier with validation context 具有自定义验证属性的模型验证 - Model validation with custom validation attribute 自定义验证属性 - Custom Validation Attribute 自定义验证属性调用另一个验证属性 - Custom validation attribute calling another validation attribute 使用自定义属性进行非负验证使用反射 - Using custom attribute for non negative validation using reflection 创建访问同一上下文中的实体的自定义验证属性时,如何解决循环引用? - How do I resolve circular reference when creating a custom validation attribute that accesses entities in the same context? 我可以使用自定义验证属性手动验证属性吗? - Can I manually validate a property using a custom validation attribute? 是否有模型验证属性可以在不使用自定义代码的情况下检查参数类型? - Is there an model validation attribute to check parameter types without using custom code? 使用数据注释和代码优先的自定义验证属性 - Custom validation attribute using data annotations and code first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM