DbExtensions supports a special column alias syntax to match columns to properties and constructor parameters. This syntax is:
- Supported for both POCO and dynamic objects
- Supported on all query APIs (using SqlBuilder, SqlSet or
string
)
Basic mapping
var db = new Database("<connection string>", "<provider invariant name>");
var query = SQL
.SELECT("*")
.FROM("Products");
return db.Map<Product>(query);
If the column names in your database exactly match your object’s properties then there’s nothing else to do. If a column name does not match a property it’s simply ignored.
Warning
It’s not recommended to project onto an annotated entity type. Updating a partially loaded entity may cause data loss.
To map to dynamic objects omit the type:
return db.Map(query);
Complex properties
Use the $
character in column aliases to specify a path into a complex property. For example,
given the types:
You can use the following query to return Product objects that include Category and Supplier:
There’s no depth limit, e.g.:
Note
If all columns related to a complex property are null, the property is set to null.
Constructors
Positional arguments
Use numbers to map columns to constructor arguments. e.g.:
This also works for complex properties, e.g:
In the example above, CompanyWebsite AS CompanyWebsite$1
means map the CompanyWebsite column to the first constructor parameter of the CompanyWebsite property, which in this case is a Uri object.
CompanyWebsite AS CompanyWebsite$1 \____________/ \____________/ \ | | \ column in property in first parameter Supplier table SupplierInfo in property's object constructor
Let’s use the Uri type again, but as parameter in SupplierInfo:
In the example above, CompanyWebsite AS 2$1
means map the CompanyWebsite column to the first constructor parameter (of Uri) of the second constructor parameter (of SupplierInfo).
Note
Numbers only have meaning relative to each other. For instance, instead of using 1 and 2, you can use 0 and 1, 100 and 200, or -68 and 17. The number of numeric column names determines which constructor to use (number of parameters), and the arguments are ordered based on the column names.
Named arguments
Starting v6.3 you can use the same syntax for complex properties to map columns to arguments by name instead of position. This only works for types that have a single public constructor.