ISqlModelMapper.ValidateTrackedModels() Method
.NET Standard 2.x
Performs validity check to all data in the model tracked by the current ISqlModelMapper
object.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public bool ValidateTrackedModels();
Returns
System.Boolean
Whether the data in the model tracked has passed the validity check.
Remarks
The validation rule is separately defined in each model class. 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.
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 Example1()
{
// The LastName is required.
// The FirstName must be more than one character.
var mapper = _context.SqlModelMapper;
var person = new PersonValidation()
{
FirstName = "Kim"
};
mapper.TrackCreate(person);
// The LastName is required. Verification failed.
bool validation = mapper.ValidateTrackedModels();
Console.WriteLine("The Validation function returns {0}", validation);
// Set LastName.
person.LastName = "Abercrombie";
// The LastName is required. Verification passed.
validation = mapper.ValidateTrackedModels();
Console.WriteLine("The Validation function returns {0}", validation);
/*The code example produces the following output:
The Validation function returns false.
The Validation function returns true.
*/
}
}
}
Example Refer To
Model Class: PersonValidation
Applies to
.NET Standard
2.x