Properties
Static mapping is performed as each property with specific attributes mark. Main attribute to statically map property is PropertyAttribute with parameters: Source - data source alias (may be omitted for virtual table), FieldName - DB field name mapped to property (may be omitted when field and property correspond naming rule, for example DB field named as some_field_name and mapped to SomeFieldName property), Flags - optional, mask of property flags,NativeSqlType - optional, SQL type name in DB. Parameter NativeSqlType is used when type default mapping is conflicted with real SQL type name.
[Property("S" /* table alias */, "code" /* field name */)]
SQL expression property
Read only property can be mapped to SQL expression. Static mapping of SQL expression is defined by PropertyExpressionAttribute with parameters: ExprText - depends of ExprType value (see below) it can be: subquery alias / SQL expression / private static field name define SQL expression in LINQ style, ExprType - optional, SQL expression type, default is DataExpressionType.PlainSql, DbType - ADO ,Net provider data type (optional, default is DbType.String), ExprSize - optional, expression field size (if applicable), ExprScale - optional, expression field precision (if applicable). Possible to use SQL expressions of three types:
- subquery,
[PropertyExpression("A",
DataExpressionType.SubQuery,
DbType.Decimal,
ExprSize = 17,
ExprScale = 5)]
- LINQ expression,
[PropertyExpression("AllowedDiscount",
DataExpressionType.LinqExpression,
DbType.Decimal,
ExprSize = 17,
ExprScale = 5)]
...
//Private static field of mapped class marked with SqlExpression attribute
[SqlExpression]
private static Calculate<int> AllowedDiscount = x => x.Case<Product, int>(
z => z.Field<bool>("is_vip"), z => 0, 1.SetExpression(z => 10));
- plain text SQL expression.
[PropertyExpression("case P.is_vip when 1 then 10 else 0 end",
DataExpressionType.PlainSql,
DbType.Decimal,
ExprSize = 17,
ExprScale = 5)]
Tip
Attribute PropertyExpressionAttribute is not replaced PropertyAttribute but extend it.
Important
If possible do not use plain SQL expressions. It possibly use specific SQL dialect features not supported by other DBMS or providers.
Tip
It's recommended to use virtual table as SQL expression source omitting data source alias in PropertyAttribute.
Property default value
To set default value of property with static mapping, use PropertyDefaultAttribute with parameters: DefaultSource - default value type, DefaultValue optional, depends on DefaultSource, DefaultFeature - optional, default is DefaultFeature.UseOnInsert, default value assignment will be applied with some extended features.
[PropertyDefault(DefaultType.UserName, AlwaysUseDefault = true)]
Property grouping parameters
Property grouping parameters used when data object mapped to grouped query to specify property role in GROUP BY expression. PropertyGroupingAttribute has properties: Grouping - optional, property aggregation type, default is None, GroupOrder - optional, property grouping order when Grouping = DataGrouping.None.
[PropertyGrouping(DataGrouping.Sum)]
Hidden properties
Hidden properties statically defined using couple of specific attributes: ColumnAttribute, ColumnExpressionAttribute and ColumnDefaultAttribute as analog with similar attributes of properties.
ColumnAttribute has parameters: PropertyName - name of hidden property, PropertyType - type of hidden property, Source - data source alias, FieldName - optional, DB field name (can be omitted when correspond default mapping rule (example: some_field_name is mapped to SomeFieldName)), Flags - optional, bit mask of property flags, Grouping - optional, defined aggregation type, default is None, GroupOrder - optional, group order when Grouping = DataGrouping.None, Hidden - optional, mapped field omitted in SELECT expression of result query (when grouping limitations demand this), default is false, NativeSqlType - optional, native SQL type name. NativeSqlType is used when default data type mapping is conflicted with result set data type.
ColumnExpressionAttribute has parameters: PropertyName - name of hidden property (same as defined by ColumnAttribute), ExprText - depends on ExprType (see below): subquery alias / SQL expression text / field name with LINQ style expression definition, ExprType - optional, SQL expression type default is DataExpressionType.PlainSql, DbType - data type on ADO .Net provider level (optional, default is DbType.String), ExprSize - optional, expression field size (if applicable), ExprScale - optional, expression field precision (if applicable).
ColumnDefaultAttribute has parameters: PropertyName - hidden property name (same as defined by ColumnAttribute), DefaultSource - default value type, DefaultValue optional, depends on DefaultSource, DefaultFeature - optional, default is DefaultFeature.UseOnInsert, default value assignment will be applied with some extended features.
[Column("DocId", typeof(long?), "D", Flags = DataPropertyFlag.Id),
ColumnDefault("DocId", DefaultType.AutoIncrement)]
Inner view reference
To map property to field from inner view a ReferenceAttribute is used with parameters: Source - inner view alias, PropertyName - optional, DB field name (may be omitted when property name is equals with inner view mapped class property), Flags - optional, mask of property flags,NativeSqlType - optional, SQL type name in DB. Parameter NativeSqlType is used when type default mapping is conflicted with real SQL type name.
[Reference("H" /* inner view alias */,
"HistoryDate" /* property name inside inner view */)]
Links
To statically define link property pairs used LinkPropertyAttribute with parameters: DictSource - optional, source object property name, Property - optional, property name. When one or both of parameters is omitted, followed rules are used:
- When omit both properties - used property name of link property as target and equal named property from source object
- When DictSource is omitted - used Property as target property name and equal named property from source object
- When Property is omitted - used property name of link property as target and property with name equals DictSource from source object
[Property("S", "name"),
LinkProperty("Name") /* DocState.Name -> this.DocState */,
LinkProperty(Property = "DocStateId") /* column DocState.DocStateId -> column this.DocStateId */,
LinkProperty("Code", "DocStateCode") /* DocState.Code -> this.DocStateCode */]
public Link<string, DocState> DocState { get; set; }