Object Relational Mapping#
Simple object-relational mapping abstraction layer for geodatabase feature classes and tables. It converts features and rows into a (lazy) sequence of strongly-typed objects.
Getting Started#
You can begin using the object-relational mappping in 2 easy steps.
-
Create a
classthat inherits from theEntityclass.- Decorate each property in the
classwith theEntityFieldattribute that you want to be mapped to column on a table.- The data type of the property should match that of the table column.
- Decorate the
classwith theEntityTableattribute that you want to have mapped to a table.
2. Use the CRUD extension methods that have been exposed on the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[EntityTable("CITY")] public class City : Entity { [EntityField("NAME")] public string Name {get;set;} [EntityField("POPULATION")] public int? Population {get;set;} [EntityField("STATE")] public string State {get;set;} [EntityField("DESCRIPTION")] public string Description {get;set} }
ITableandIFeatureClassinterfaces andEntityclass. - Decorate each property in the
CRUD#
An entity object can be used to C-reate, R-ead, U-pdate and D-elete either IRow or IFeature objects by using methods exposed on the Entity object or (ITable and IFeatureClass) interfaces.
-
Create: Create new records using entity objects.
Class Method Description EntityInsert(ITable) Inserts the entity object into the table using an insert cursor. EntityInsert(IFeatureClass) Inserts the entity object into the feature class using an insert cursor.
2. Read: Load data from a table into entity objects.1 2 3 4 5 6 7 8 9 10
var cities = workspace.GetTable(typeof(City)); var denver = new City { Name = "Denver", Population = 663862, State = "Colorado", Description = "The capital of Colorado." }; denver.Insert(cities);
Interface Method Description ITableMap(IQueryFilter) Reads the database rows as a (lazily-evaluated) sequence of objects that are converted into the entity type. IFeatureCLassMap(IQueryFilter) Reads the database features as a (lazily-evaluated) sequence of objects that are converted into the entity type.
3. Update: Flush property changes to the underlying row in the database.1 2 3 4 5 6 7 8 9 10 11
var filter = new QueryFilter(); filter.Where = "State = 'Colorado'; var cities = workspace.GetTable(typeof(City)); foreach (var city in cities.Map<City>(filter) { Console.WriteLine("Name: {0}" , city.Name); Console.WriteLine("POP: {0}" , city.Population); Console.WriteLine("State: {0}" , city.State); Console.WriteLine("Description : {0}", city.Description); }
Class Method Description EntityUpdate() Updates the underlying backing object (either IRow or IFeature) 1 2 3 4 5 6 7
var cities = workspace.GetTable(typeof(City)); var filter = new QueryFilter(); filter.Where = "Name = 'Denver'"; var denver = cities.Map<City>(filter).SingleOrDefault(); denver.Description = "Denver, the capital of Colorado, is an American metropolis dating to the Old West era. Larimer Square, the city’s oldest block, features landmark 19th-century buildings."; denver.Update();
-
Delete: Remove records from the table using the entity object.
Class Method Description EntityDelete() Deletes the underlying backing object (either IRow or IFeature) 1 2 3 4 5 6 7
var cities = workspace.GetTable(typeof(City)); var filter = new QueryFilter(); filter.Where = "Name = 'Denver'"; var denver = cities.Map<City>(filter).SingleOrDefault(); denver.Delete(); denver = null;