StorageContext

For information on how to use StorageContext, see Using StorageContext

Constructor

The StorageContext contructor takes exactly one required parameter: an IDataProvider object. Every StorageContext points to exactly one database.

Note that it's possible to create StorageContext using the Iridium Core ServiceLocator by registering a data provider and calling ServiceLocator.Create<StorageContext>().

In a real application, StorageContext should be subclassed. See also Using StorageContext

Methods

Although StorageContext is the main interface to talk to a database, the methods in this class are usually not called directly. Most database access is done via the IDataSet<T> interface.

Method Description
Read<T>() Read (and return) a single object from the database
Load<T>() Read data from the database into a single object
Delete<T>() Delete objects from the database
Insert() Insert one or more objects in the database
Update() Update one or more objects in the databaae
InsertOrUpdate() Insert or update one or more objects in the database
UpdateOrCreateOnlyRecord<T> Insert/update record in a table with one record
DataSet<T>() Get a dataset for a particular object type
LoadRelations<T>() Load relations for one or more entities
LoadRelation() Load a single relation and return it
CreateTransaction() Create/start a transaction
RunTransaction() Run code inside a transaction
SqlQuery() Run a native SQL query (returning data)
SqlNonQuery() Run a native SQL query (not returning any data)
SqlQueryScalar() Run a native SQL query returning a single value
SqlQueryScalars() Run a native SQL query returning a list of single values
CreateTable<T>() Create or update a table in the database

All methods listed above are also available as async. Add Async to the name of the method for the asynchronous method, for example InsertAsync()

T Read<T>()

Reads a record of type T from the database

T Read<T>(object key)

Reads a record with the given primary key. For tables with a composite primary key, an anonymous object should be passed as the key:

var obj = db.Read<MyClass>(new {Key1=1,Key2=2});

T Read<T>(Expression filter)

Reads the first record satisfying the filter condition.

The filter condition should only return one record. If it returns more than one record, it is undefined which record will be returned.

Example:

var customer = db.Read<Customer>(c => c.Name == "John Doe");

T Load<T>()

Loads a record of type T into an existing object, updating all object properties with fields from the database

T Load<T>(T obj, object key)