Working with database entities
Glossary Item Box
Examples of using the Entity class to work with the database
You can download the package with the configuration web service implementing the cases described below using the following link.
Example 1
public string GetEntityColumnData() { var result = ""; // Creating a request to the City schema, adding the Name column to the request. var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "City"); var colName = esqResult.AddColumn("Name"); // Running the request to the database and receiving an object with the specified identifier. You can receive the UId of the object from the browser navigation bar of the open record edit page. var entity = esqResult.GetEntity(UserConnection, new Guid("100B6B13-E8BB-DF11-B00F-001D60E938C6")); // Receiving the value of the object column. result += entity.GetColumnValue(colName.Name).ToString(); return result; }
Example 2
Receiving the name collection of the [City] schema column.
public IEnumerable<string> GetEntityColumns() { // Creating the data string object of the City schema (using the schema Id received from the database). var entity = new Entity(UserConnection, new Guid("5CA90B6A-93E7-4448-BEFE-AB5166EC2CFE")); // Receiving the object with the specified id from the database. You can receive the UId of the object from the browser navigation bar of the open record edit page. entity.FetchFromDB(new Guid("100B6B13-E8BB-DF11-B00F-001D60E938C6"),true); // Receiving the name collection of the object columns. var result = entity.GetColumnValueNames(); return result; }
Example 3
public bool DeleteEntity() { // Creating a request to the Order schema, adding all schema columns to the request. var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Order"); esqResult.AddAllSchemaColumns(); // Running the request to the database and receiving the object with the specified id. You can receive the UId of the object from the browser navigation bar of the open record edit page. var entity = esqResult.GetEntity(UserConnection, new Guid("e3bfa32f-3fe9-4bae-9332-16c162c51e0d")); // Deleting the object from the database. entity.Delete(); // Verification whether the object with the specified Id exists in the database. var result = entity.ExistInDB(new Guid("e3bfa32f-3fe9-4bae-9332-16c162c51e0d")); return result; }
Example 4
public bool UpdateEntity() { // Creating a request to the Order schema, adding all schema columns to the request. var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Order"); esqResult.AddAllSchemaColumns(); // Running the request to the database and receiving the object with the specified id. You can receive the UId of the object from the browser navigation bar of the open record edit page. var entity = esqResult.GetEntity(UserConnection, new Guid("58be5223-715d-4b16-a5c4-e3d4ec0412d9")); // Creating a data string object of the OrderStatus schema. var statusSchema = UserConnection.EntitySchemaManager.GetInstanceByName("OrderStatus"); var newStatus = statusSchema.CreateEntity(UserConnection); // Receiving the object with specified name from the database. newStatus.FetchFromDB("Name", "4. Completed"); // Assigns a new value to the StatusId column. entity.SetColumnValue("StatusId", newStatus.GetTypedColumnValue<Guid>("Id")); // Saving the changed object in the database. var result = entity.Save(); return result; }
Example 5
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); // Sets default values for the object columns. entity.SetDefColumnValues(); var contryEntity = new Entity(UserConnection, new Guid("09FCE1F8-515C-4296-95CD-8CD93F79A6CF")); contryEntity.FetchFromDB("Name", country); // Assigns the passed-in city name to the Name column. entity.SetColumnValue("Name", city); // Assings the UId of the passed-in country to the CountryId UId column. entity.SetColumnValue("CountryId", contryEntity.GetTypedColumnValue<Guid>("Id")); var result = entity.Save(); return result; }
See also: