In order to write changes to the database you need to call the SaveChanges method of the OpenAccessContext instance.
| C# |
Copy Code |
|
dbContext.SaveChanges(); |
| VB.NET |
Copy Code |
|
dbContext.SaveChanges() |
How to Get Changes that Will be Performed During the Next Commit
This section provides an example of how to get all changes that will be performed during the next commit (during the next call of the SaveChanges method). In the following example, a LINQ query returns a single Category object based on a specified CategoryName. Then, the Description property of the category is changed, two new category objects are added to the context, and the newly added items are deleted. The GetChanges method is called to get all pending inserts, deletes or updates.
| C# |
Copy Code |
|
using (EntitiesModel dbContext = new EntitiesModel()) { // Update a category Category category = dbContext.Categories.First(); category.CategoryName = "New Name";
// Create two new categories Category newCategory1 = new Category(); newCategory1.CategoryName = "New Category"; Category newCategory2 = new Category(); newCategory2.CategoryName = "New Category2";
dbContext.Add(new Category[] { newCategory1, newCategory2 });
dbContext.Delete(new Category[] { newCategory1, newCategory2 });
// GetChanges Telerik.OpenAccess.ContextChanges contextChanges = dbContext.GetChanges(); IList<Category> inserts = contextChanges.GetInserts<Category>(); IList<Category> updates = contextChanges.GetUpdates<Category>(); IList<Category> deletes = contextChanges.GetDeletes<Category>();
Console.WriteLine("{0} objects will be inserted", inserts.Count); Console.WriteLine("{0} objects will be updated", updates.Count); Console.WriteLine("{0} objects will be delete", deletes.Count); } |
| VB.NET |
Copy Code |
|
Using dbContext As New EntitiesModel() Dim category_Renamed As Category = dbContext.Categories.First() category_Renamed.CategoryName = "New Name"
Dim newCategory1 As New Category() newCategory1.CategoryName = "New Category" Dim newCategory2 As New Category() newCategory2.CategoryName = "New Category2"
dbContext.Add(New Category() {newCategory1, newCategory2}) dbContext.Delete(New Category() {newCategory1, newCategory2})
Dim contextChanges As Telerik.OpenAccess.ContextChanges = dbContext.GetChanges() Dim inserts As IList(Of Category) = contextChanges.GetInserts(Of Category)() Dim updates As IList(Of Category) = contextChanges.GetUpdates(Of Category)() Dim deletes As IList(Of Category) = contextChanges.GetDeletes(Of Category)()
Console.WriteLine("{0} objects will be inserted", inserts.Count) Console.WriteLine("{0} objects will be updated", updates.Count) Console.WriteLine("{0} objects will be delete", deletes.Count) End Using |
Further References