ISqlModelMapper.ValidateTrackedModels(out List<ValidationResult> ValidationResults) Method
.NET Standard 2.x
Performs validity check to all data in the model tracked by the current ISqlModelMapper
object and gets the validation result.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public bool ValidateTrackedModels(out List<ValidationResult> ValidationResults);
Parameters
result
System.Collections.Generic.List<System.ComponentModel.DataAnnotations.ValidationResult>
A List<ValidationResult>
object that includes the data validation results.
Returns
System.Boolean
Whether all of the data in the model object tracked has passed the validity check.
Remarks
The validation rule is separately defined in each model. It evaluates each ValidationAttribute instance that is attached to the model, and validates the property values of the model.
When the IModeMapper.SaveChanges method is executed, it will also validate all of the data in the model object tracked. If validation failed, it will throw a ValidationException
object.
Examples
The following code example demonstrates how to perform validity check and get the result.
using Appeon.ApiDoc.Models.School;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class ValidateTrackedModelsExample
{
private SchoolContext _context;
public ValidateTrackedModelsExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example2()
{
// The LastName is required.
// The FirstName must be more than one character.
var mapper = _context.SqlModelMapper;
var person = new PersonValidation() { FirstName = "a" };
var results = new List<ValidationResult>();
mapper.TrackCreate(person);
bool isValid = mapper.ValidateTrackedModels(out results);
Console.WriteLine("The Validate function returns {0}.", isValid);
if (!isValid)
{
foreach (var validationResult in results)
{
Console.WriteLine("ErrorMessage:{0}",
validationResult.ErrorMessage);
}
}
/*The code example produces the following output:
The Validate function returns False.
ErrorMessage:The LastName field is required.
ErrorMessage:The field FirstName must be a string or array type with a
minimum length of '2'.
*/
}
}
}
Example Refer To
Model Class: PersonValidation
Applies to
.NET Standard
2.x