EF Core2.0 dbvalidation错误未显示所有错误

时间:2018-07-05 18:25:52

标签: asp.net-core ef-core-2.0

我已经创建了一个EF core 2.0应用程序,并尝试在Savechanges上验证该模型,但它仅返回第一个验证错误。

这是我的Dbcontext和控制器

public partial class ProductWarehouseContext : DbContext
{  public List<string> ErrorList=new List<string>();
    public ProductWarehouseContext(DbContextOptions<ProductWarehouseContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Customer> Customer { get; set; }
    public virtual DbSet<Order> Order { get; set; }
    public virtual DbSet<OrderItem> OrderItem { get; set; }
    public virtual DbSet<Product> Product { get; set; }
    public virtual DbSet<Supplier> Supplier { get; set; }


    public override int SaveChanges()
    {
        var entities = from e in ChangeTracker.Entries()
            where e.State == EntityState.Added
                  || e.State == EntityState.Modified
            select e.Entity;


        foreach (var entity in entities)
        {
            var validationContext = new ValidationContext(entity);
            Validator.ValidateObject(
                entity,
                validationContext,
                validateAllProperties: true);
        }

        return base.SaveChanges();}
    }

控制器

[HttpPost]
    public IActionResult Save([FromBody]CustomerViewModel customer)
    {
        using (var cont = _context.Database.BeginTransaction())
        {
            try
            {
                var cust = new Customer()
                {
                    FirstName = customer.FirstName,
                    LastName = customer.LastName,
                    City = customer.City,
                    Country = customer.Country,
                    Phone = customer.Phone,
                    IsSubscribedforAlerts = customer.IsSubscribedforAlerts
                };

                _context.Customer.Add(cust);
                _context.SaveChanges();
                cont.Commit();
            }

            catch (Exception e)
            {
                Errors.Add(e.Message);
                cont.Rollback();
                foreach (var err in Errors)
                {
                    ModelState.AddModelError("errors", err);
                }
                return Ok(ModelState);
            }
        }
        return Ok();
    }

班级

public partial class Customer
{
    public Customer()
    {
        Order = new HashSet<Order>();
    }


    public int Id { get; set; }

    [Required(ErrorMessage = "FirstName is required to save a new customer")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "LastName is required to save a new customer")]
    public string LastName { get; set; }

    public string City { get; set; }

    public string Country { get; set; }

    [Required(ErrorMessage = "PhoneNumber is required to save a new customer")]
    public string Phone { get; set; }


    public bool? IsSubscribedforAlerts { get; set; }

    public ICollection<Order> Order { get; set; }
}

,错误仅在于returnig““ firstname”是必需的,如果我在请求对象中传递名字,则其返回的“ lastname”是必需的。 我该怎么做才能返回所有错误,如何使用DbEntityValidationException在EF6中做到这一点?

1 个答案:

答案 0 :(得分:0)

那是因为ValidateObject()在第一次遇到错误时抛出。尝试改用TryValidateObject(),然后将累积所有错误的List<ValidationResult>传递给它。

类似的东西:

public class EntityValidationException : Exception
{
    public EntityValidationException(IEnumerable<ValidationException> exceptions)
    {
        this.ValidationErrors = exceptions;
    }

    public IEnumerable<ValidationException> ValidationErrors { get; }
}

然后在您的SaveChanges()中:

foreach (var entity in entities)
{
    var validationContext = new ValidationContext(entity);
    var validationResults = new List<ValidationResult>();

    Validator.TryValidateObject(entity, validationContext, validationResults);

    if (validationResults.Any())
        throw new EntityValidationException(validationResults.Select(x => new ValidationException(x, null, null)));
}

然后,在您的控制器/操作中,您可以显式处理EntityValidationException

catch (EntityValidationException validationException)
{
    foreach (var err in validationException.ValidationErrors)
    {
        var validationResult = err.ValidationResult;

        ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
    }
}