Performing Insert, Update, and Delete Operations

Insert, delete, and update operations on GemFire XD data can be executed through an ObjectContext object in the Entity Framework.

Querying Tables and Views

To perform a query you use the ObjectContext to create an ObjectQuery, which you can then use to create a query using LINQ to Entities. Executing the query against the GemFire XD datasource returns either a single object or a collection of objects, which you can use to access the query results. Any GemFire XD errors that occur while executing the query are passed directly to the client.

The following example shows a simple LINQ to Entities query in C#:
var context = new StudentEntities();
var query = from c in context.STUDENT select c; 
var students = query.ToList(); 

foreach (STUDENT a in students) 
{ 
  Console.WriteLine("Student Name: {0}", a.Name); 
} 

Console.ReadLine();

Inserting Rows

You can insert rows by creating new entity object representations through the code, and then adding those objects to the ObjectContext. Calling SaveChanges() on the ObjectContext internally generates and executes commands to perform the equivalent INSERT statement using the GemFire XD data source. Ensure that you set all object properties that are required by the underlying table definition.

The following C# example shows four new student rows being added to the STUDENT table:
var context = new StudentEntities();

STUDENT student1 = new STUDENT { ID = "101", NAME = "Name1" }; 
  context.STUDENTS.AddObject(student1); 
STUDENT student2 = new STUDENT { ID = "102", NAME = "Name2" }; 
  context.STUDENTS.AddObject(student2); 
STUDENT student3 = new STUDENT { ID = "103", NAME = "Name3" }; 
  context.STUDENTS.AddObject(student3); 
STUDENT student4 = new STUDENT { ID = "104", NAME = "Name4" }; 
  context.STUDENTS.AddObject(student4); 

context.SaveChanges();

Updating Rows

Calling SaveChanges() on the ObjectContext inserts, updates, or deletes rows in the underlying GemFire XD table depending on how you have modified entities in the context. Updates are performed by first querying the context and then modifying one or more properties of the returned entities before saving the context. For example:
var context = new StudentEntities();

var students = from c in context.STUDENT where c.Name == "Name2" select c; 

var student = students.FirstOrDefault(); 
student.Name = "Name7"; 

context.SaveChanges();

Deleting Rows

To delete a row from a table, use the DeleteObject() method of the ObjectContext to delete an entity instance. The object is immediately removed from the collection of its type, but it is not yet destroyed. Call SaveChanges() on the ObjectContext to delete the row from the table. For example:
var context = new StudentEntities();

var students = from c in context.STUDENT where c.Name == "Name3" select c; 

var student = students.FirstOrDefault(); 

context.DeleteObject(student); 

context.SaveChanges();