Manage the database entities
Level: advanced
note
Examples 1-5 in this article are also implemented in the web service. The package that contains the web service implementation is attached in the Resources block.
Example 1
Example
Retrieve the value of the City schema column whose name is Name
.
GetEntityColumnData() method
public string GetEntityColumnData() {
var result = "";
/* Create a query to the City schema, add the Name column to the query. */
var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "City");
var colName = esqResult.AddColumn("Name");
/* Run the database query and retrieve the object that has the set identifier. You can retrieve the object UId from the browser navigation bar after opening the section record page. */
var entity = esqResult.GetEntity(UserConnection, new Guid("100B6B13-E8BB-DF11-B00F-001D60E938C6"));
/* Retrieve the object column value. */
result += entity.GetColumnValue(colName.Name).ToString();
return result;
}
Example 2
Example
Retrieve the name collection of the City schema columns.
GetEntityColumns() method
public IEnumerable <string> GetEntityColumns() {
/* Create a data string object of the City schema based on the schema identifier retrieved from the database. */
var entity = new Entity(UserConnection, new Guid("5CA90B6A-93E7-4448-BEFE-AB5166EC2CFE"));
/* Retrieve the object that has the set identifier from the database. You can retrieve the object UId from the browser navigation bar after opening the section record page. */
entity.FetchFromDB(new Guid("100B6B13-E8BB-DF11-B00F-001D60E938C6"), true);
/* Retrieve the name collection of the object columns. */
var result = entity.GetColumnValueNames();
return result;
}
Example 3
Example
Delete the Order schema records from the database.
DeleteEntity() method
public bool DeleteEntity() {
/* Create a query to the Order schema, add all schema columns to the query. */
var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Order");
esqResult.AddAllSchemaColumns();
/* Run the database query and retrieve the object that has the set identifier. You can retrieve the object UId from the browser navigation bar after opening the section record page. */
var entity = esqResult.GetEntity(UserConnection, new Guid("e3bfa32f-3fe9-4bae-9332-16c162c51e0d"));
/* Delete the object from the database. */
entity.Delete();
/* Check if the object that has the set identifier exists in the database. */
var result = entity.ExistInDB(new Guid("e3bfa32f-3fe9-4bae-9332-16c162c51e0d"));
return result;
}
Example 4
Example
Change the order status.
UpdateEntity() method
public bool UpdateEntity() {
/* Create a query to the Order schema, add all schema columns to the query. */
var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Order");
esqResult.AddAllSchemaColumns();
/* Run the database query and retrieve the object that has the set identifier. You can retrieve the object UId from the browser navigation bar after opening the section record page. */
var entity = esqResult.GetEntity(UserConnection, new Guid("58be5223-715d-4b16-a5c4-e3d4ec0412d9"));
/* Create the data string object of the OrderStatus schema. */
var statusSchema = UserConnection.EntitySchemaManager.GetInstanceByName("OrderStatus");
var newStatus = statusSchema.CreateEntity(UserConnection);
/* Retrieve the object that has the set identifier from the database. */
newStatus.FetchFromDB("Name", "4. Completed");
/* Assign the new value to the StatusId column. */
entity.SetColumnValue("StatusId", newStatus.GetTypedColumnValue <Guid> ("Id"));
/* Save the modified object to the database. */
var result = entity.Save();
return result;
}
Example 5
Example
Add the city that has the specified name and connect it to the specified country.
UpdateEntity() method
public bool InsertEntity(string city, string country) {
city = city ?? "unknown city";
country = country ?? "unknown country";
var citySchema = UserConnection.EntitySchemaManager.GetInstanceByName("City");
var entity = citySchema.CreateEntity(UserConnection);
entity.FetchFromDB("Name", city);
/* Set the default values for the object columns. */
entity.SetDefColumnValues();
var contryEntity = new Entity(UserConnection, new Guid("09FCE1F8-515C-4296-95CD-8CD93F79A6CF"));
contryEntity.FetchFromDB("Name", country);
/* Assign the passed city name to the Name column. */
entity.SetColumnValue("Name", city);
/* Assign the passed country UId to the CountryId column. */
entity.SetColumnValue("CountryId", contryEntity.GetTypedColumnValue <Guid> ("Id"));
var result = entity.Save();
return result;
}
Example 6
Example
Create a contact that has the "User01" name.
EntitySchema contactSchema = UserConnection.EntitySchemaManager.GetInstanceByName("Contact");
Entity contactEntity = contactSchema.CreateEntity(UserConnection);
contactEntity.SetDefColumnValues();
contactEntity.SetColumnValue("Name", "User01");
contactEntity.Save();
Example 7
Example
Change the contact name to "User02".
EntitySchema entitySchema = UserConnection.EntitySchemaManager.GetInstanceByName("Contact");
Entity entity = entitySchema.CreateEntity(UserConnection);
if(!entity.FetchFromDB(some_id)) {
return false;
}
entity.SetColumnValue("Name", "User02");
return entity.Save();
Example 8
Example
Delete the contact that has the "User02" name.
EntitySchema entitySchema = UserConnection.EntitySchemaManager.GetInstanceByName("Contact");
Entity entity = entitySchema.CreateEntity(UserConnection);
var fetchConditions = new Dictionary<string, object> {
{"Name", "User02"}
};
if (entity.FetchFromDB(fetchConditions)) {
entity.Delete();
}