ISqlModelMapper.SaveChanges() Method
.NET Standard 2.x
Saves all of the data changes to the database, by executing the database table operations tracked by the ISqlModelMapper
object, and calls the tracked Action
delegates during operations.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public IDbResult SaveChanges();
Returns
Returns an IDbResult
object whose property can be used to conveniently get the result of the executed database operation.
Remarks
When the database is updated successfully, SqlModelMapper is restored to its initial state which will not track data changes.
To continue using SqlModelMapper for other data operations, use ISqlModelMapper.TrackCreate method etc. to track the changes again.
Database Transaction
This method will create by default a database transaction where all of the SQL DML will be executed.
When any exceptions occur, the database transaction will be automatically rolled back.
Validation
Before updating the database, the system will validate the data in the model. If detects any error, exceptions will be thrown.
Examples
The following code example demonstrates how to register two new students.
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class SaveChangesExample
{
private SchoolContext _context;
public SaveChangesExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
var mapper = _context.SqlModelMapper;
// Tracks the two new students and saves changes to the database.
var personA = new Person()
{
LastName = "Senior",
FirstName = "Letitia",
EnrollmentDate = DateTime.Now,
Discriminator = "Student"
};
var personB = new Person()
{
LastName = "Ward",
FirstName = "Lilith",
EnrollmentDate = DateTime.Now,
Discriminator = "Student"
};
var dbResult = mapper.TrackCreate(personA)
.TrackCreate(personB)
.SaveChanges();
Console.WriteLine("{0} records have been inserted into the database.",
dbResult.InsertedCount);
/* The code produces the following output:
2 records have been inserted into the database.
*/
}
}
}
Example Refer To
Model Class: Person
Applies to
.NET Standard
2.x