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
class
that inherits from theEntity
class.- Decorate each property in the
class
with theEntityField
attribute 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
class
with theEntityTable
attribute that you want to have mapped to a table.
1. 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} }
ITable
andIFeatureClass
interfaces andEntity
class. - 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 :-----------------------|:------------------------------|:----------------------|
Entity
| Insert(ITable) | Inserts the entity object into the table using an insert cursor.Entity
| Insert(IFeatureClass) | Inserts the entity object into the feature class using an insert cursor.
1. 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 :-----------------------|:------------------------------|:----------------------|
ITable
| Map(IQueryFilter) | Reads the database rows as a (lazily-evaluated) sequence of objects that are converted into the entity type.IFeatureCLass
| Map(IQueryFilter) | Reads the database features as a (lazily-evaluated) sequence of objects that are converted into the entity type.
1. 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 :-----------------------|:------------------------------|:----------------------|
Entity
| Update() | 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 :-----------------------|:------------------------------|:----------------------|
Entity
| Delete() | 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;