Class AssociationAttribute
Defines relation between tables or views. Could be applied to:
- instance properties and fields;
- instance and static methods.
For associations, defined using static methods, this
mapping side defined by type of first parameter.
Also, optionally, you can pass data context object as extra method parameter.
Based on association type - to one or to multiple records - result type should be target record's mapping type or IEquatable<T> collection.
By default associations are used only for joins generation in LINQ queries and will have null
value for loaded
records. To load data into association, you should explicitly specify it in your query using LoadWith<TEntity, TProperty>(IQueryable<TEntity>, Expression<Func<TEntity, TProperty?>>) method.
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
public class AssociationAttribute : MappingAttribute, _Attribute
- Inheritance
-
AssociationAttribute
- Implements
- Inherited Members
- Extension Methods
Constructors
AssociationAttribute()
Creates attribute instance.
public AssociationAttribute()
Properties
AliasName
Gets or sets alias for association. Used in SQL generation process.
public string? AliasName { get; set; }
Property Value
AssociationSetterExpression
Specifies a setter expression. If is set, it will be used to set the storage member when using LoadWith(). Action takes two parameters: the storage member and the value to assign to it.
Expression<Action<SomeContainerType,SomeOtherEntity>> setContainerValue;
setContainerValue = (container, value) => container.Value = value;</code></pre></example></p>
public Expression? AssociationSetterExpression { get; set; }
Property Value
AssociationSetterExpressionMethod
Specifies static property or method without parameters, that returns a setter expression. If is set, it will be used to set the storage member when using LoadWith(). Result of method should be Action which takes two parameters: the storage member and the value to assign to it.
public class SomeEntity
{
[Association(SetExpressionMethod = nameof(OtherImpl), CanBeNull = true)]
public SomeOtherEntity Other { get; set; }
public static Expression<Action<SomeContainerType,SomeOtherEntity>> OtherImpl()
{
return (container, value) => container.Value = value;
}
}</code></pre></example></p>
public string? AssociationSetterExpressionMethod { get; set; }
Property Value
CanBeNull
Defines type of join:
- inner join for
CanBeNull = false
; - outer join for
CanBeNull = true
. When using Configuration.UseNullableTypesMetadata, the default value for associations (cardinality 1) is derived from nullability. Otherwise the default value istrue
(for collections and when option is disabled).
public bool CanBeNull { get; set; }
Property Value
ExpressionPredicate
Specifies static property or method without parameters, that returns join predicate expression. This predicate will be used together with ThisKey/OtherKey join keys, if they are specified. Predicate expression lambda function takes two parameters: this record and other record and returns boolean result.
public string? ExpressionPredicate { get; set; }
Property Value
OtherKey
Gets or sets comma-separated list of association key members on another side of association. Those keys will be used for join predicate generation and must be compatible with ThisKey keys. You must specify keys it you do not use custom predicate (see ExpressionPredicate).
public string? OtherKey { get; set; }
Property Value
Predicate
Specifies predicate expression. This predicate will be used together with ThisKey/OtherKey join keys, if they are specified. Predicate expression lambda function takes two parameters: this record and other record and returns boolean result.
public Expression? Predicate { get; set; }
Property Value
QueryExpression
Specifies query expression. If is set, other association keys are ignored. Lambda function takes two parameters: this record, IDataContext and returns IQueryable result.
Expression<Func<SomeEntity, IDataContext, IQueryable<SomeOtherEntity>>> associationQuery;
associationQuery = (e, db) => db.GetTable<SomeOtherEntity>().Where(se => se.Id == e.Id);</code></pre></example></p>
public Expression? QueryExpression { get; set; }
Property Value
QueryExpressionMethod
Specifies static property or method without parameters, that returns IQueryable expression. If is set, other association keys are ignored. Result of query method should be lambda which takes two parameters: this record, IDataContext and returns IQueryable result.
public class SomeEntity
{
[Association(ExpressionQueryMethod = nameof(OtherImpl), CanBeNull = true)]
public SomeOtherEntity Other { get; set; }
public static Expression<Func<SomeEntity, IDataContext, IQueryable<SomeOtherEntity>>> OtherImpl()
{
return (e, db) => db.GetTable<SomeOtherEntity>().Where(se => se.Id == e.Id);
}
}</code></pre></example></p>
public string? QueryExpressionMethod { get; set; }
Property Value
Storage
Specify name of property or field to store association value, loaded using LoadWith<TEntity, TProperty>(IQueryable<TEntity>, Expression<Func<TEntity, TProperty?>>) method. When not specified, current association member will be used.
public string? Storage { get; set; }
Property Value
ThisKey
Gets or sets comma-separated list of association key members on this side of association. Those keys will be used for join predicate generation and must be compatible with OtherKey keys. You must specify keys it you do not use custom predicate (see ExpressionPredicate).
public string? ThisKey { get; set; }
Property Value
Methods
GetObjectID()
Returns mapping attribute id, based on all attribute options.
public override string GetObjectID()
Returns
GetOtherKeys()
Returns OtherKey value as a list of key member names.
public string[] GetOtherKeys()
Returns
- string[]
List of key members.
GetThisKeys()
Returns ThisKey value as a list of key member names.
public string[] GetThisKeys()
Returns
- string[]
List of key members.