.NET Core - 在模型中获取 DbContext
.NET Core - Get DbContext within Model
我需要在模型中获取 ApplicationDbContext。
我正在创建一个 ValidationAttribute。该验证将检查数据库中的一些记录并在必要时生成错误。
public class MyModel
{
public int? ID { get; set; }
[MyValidationForName]
public string Name { get; set; }
}
public class MyValidationForName : ValidationAttribute
{
protected ValidationResult IsValid(object value, ValidationContext validationContext)
{
/* code here */
return new ValidationResult("This field contains an error");
}
}
上面的例子在保存记录时产生错误(DataAnnotation)。只检查日期、长度等就可以了
但是我必须查询数据库中的一些记录。要执行此操作,无法将数据库上下文传递给模型。
我读过的所有 SO 主题都解释了如何使用 Remote 和 JS 进行验证,并将验证放在控制器上。我不要这个。
最相关的 SO 主题是:Using DB Context in a Custom Validation Attribute(添加上下文仍然无效)
任何人都可以帮助我将 ApplicationDbContext 传递给模型以执行查询并使用 ValidationAttribute 对其进行验证吗?
我从未找到答案,即使在 Stack Overflow 上也是如此。但是很多天后,我得到了解决方案。所以,我在下面回答,希望对您有所帮助。
首先,在您的模型中添加 using System.Linq;
class。
因此,应用上下文:
var _context = (ApplicationDbContext)validationContext.GetService(typeof(ApplicationDbContext));
现在我们可以在 ValidationAttribute 中执行查询了。
完整示例:
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;
public class MyModel
{
public int? ID { get; set; }
[Required(ErrorMessage = "Name can not be blank")]
[ExampleValidation]
public string Name { get; set; }
}
public class ExampleValidation : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var _context = (ApplicationDbContext)validationContext
.GetService(typeof(ApplicationDbContext));
/* my query here using the _context, and if necessary, apply the error message below: */
return new ValidationResult("My error message here");
}
}
}
我需要在模型中获取 ApplicationDbContext。
我正在创建一个 ValidationAttribute。该验证将检查数据库中的一些记录并在必要时生成错误。
public class MyModel
{
public int? ID { get; set; }
[MyValidationForName]
public string Name { get; set; }
}
public class MyValidationForName : ValidationAttribute
{
protected ValidationResult IsValid(object value, ValidationContext validationContext)
{
/* code here */
return new ValidationResult("This field contains an error");
}
}
上面的例子在保存记录时产生错误(DataAnnotation)。只检查日期、长度等就可以了
但是我必须查询数据库中的一些记录。要执行此操作,无法将数据库上下文传递给模型。
我读过的所有 SO 主题都解释了如何使用 Remote 和 JS 进行验证,并将验证放在控制器上。我不要这个。
最相关的 SO 主题是:Using DB Context in a Custom Validation Attribute(添加上下文仍然无效)
任何人都可以帮助我将 ApplicationDbContext 传递给模型以执行查询并使用 ValidationAttribute 对其进行验证吗?
我从未找到答案,即使在 Stack Overflow 上也是如此。但是很多天后,我得到了解决方案。所以,我在下面回答,希望对您有所帮助。
首先,在您的模型中添加 using System.Linq;
class。
因此,应用上下文:
var _context = (ApplicationDbContext)validationContext.GetService(typeof(ApplicationDbContext));
现在我们可以在 ValidationAttribute 中执行查询了。
完整示例:
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;
public class MyModel
{
public int? ID { get; set; }
[Required(ErrorMessage = "Name can not be blank")]
[ExampleValidation]
public string Name { get; set; }
}
public class ExampleValidation : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var _context = (ApplicationDbContext)validationContext
.GetService(typeof(ApplicationDbContext));
/* my query here using the _context, and if necessary, apply the error message below: */
return new ValidationResult("My error message here");
}
}
}