ISaveContext Interface
.NET Standard 2.x
Represents the save context when saving the data changes to the database.
It provides a series of methods for reading or manipulating the save context.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public interface ISaveContext;
Properties
Name | Returns Type | Description |
---|---|---|
Count | int | Gets the number of the added values in the context of saving the data. |
LastDbResult | IDbResult | Gets the result of the most recent operation that uses the model to save data to the database. |
Names | IReadOnlyList<string> | Gets the name of all of the added values. |
Methods
Name | Returns Type | Description |
---|---|---|
Add(string name, object value) | void | Adds a value of any data type and its name to the save context. |
CancelSave() | void | Cancels the operation that is currently being executed to save the data to the database. |
Contains(string name) | bool | Returns whether the save context contains the specified value. |
Get(string name) | object | Gets the specified value from the save context. |
Remove(string name) | void | Removes the specified value from the save context. |
RemoveAll() | void | Removes all of the added values from the save context. |
Examples
The following code example demonstrates how to use ISaveContext
to read and manipulate the save context when saving the student.
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.ISaveContextExamples
{
public class ISaveContextExample
{
private SchoolContext _context;
public ISaveContextExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
var mapper = _context.SqlModelMapper;
// Sets the properties for the new student.
var newStudent = new Person()
{
LastName = "Senior",
FirstName = "Letitia",
EnrollmentDate = new DateTime(2019, 1, 1),
Discriminator = "Student"
};
// Tracks the Insert operation for the new student.
mapper.TrackCreate(newStudent);
// Adds names to Save Context in an Action.
mapper.Track((saveContext) =>
{
// Adds a value to Save Context.
saveContext.Add("newPersonID", newStudent.PersonID);
// Gets the last database result from Save Context.
var lastResult = saveContext.LastDbResult;
saveContext.Add("Affected", lastResult.AffectedCount);
// Shows how many names were added in Save Context.
Console.WriteLine("{0} Names:",
saveContext.Count);
// Lists the names added in Save Context.
foreach (string name in saveContext.Names)
{
Console.WriteLine(name);
}
});
// Reads and removes names in another Action.
mapper.Track((saveContext) =>
{
// Checks if it contains a name.
Console.WriteLine("Check if it contains 'newPersonID': {0}",
saveContext.Contains("newPersonID"));
// Gets the value.
int newID = (int)saveContext.Get("newPersonID");
Console.WriteLine("The Person ID generated by the database is {0}.",
newID);
// Removes a name.
saveContext.Remove("newPersonID");
// Removes all names.
saveContext.RemoveAll();
// Cancels the save operation and roll back the database transaction.
saveContext.CancelSave();
});
var dbResult = mapper.SaveChanges();
Console.WriteLine("Check if the SaveChanges method was cancelled: {0}",
dbResult.Cancelled);
/* The code produces the following output:
2 Names:
newPersonID
Affected
Check if it contains 'newPersonID': True
The Person ID generated by the database is 75.
Check if the SaveChanges method was cancelled: True
*/
}
}
}
Example Refer To
Model Class: Student