Table of Contents

Class MappingSchema

Namespace
LinqToDB.Mapping
Assembly
linq2db.dll

Mapping schema.

public class MappingSchema : IConfigurationID
Inheritance
MappingSchema
Implements
Derived
Extension Methods

Constructors

MappingSchema()

Creates mapping schema instance.

public MappingSchema()

MappingSchema(params MappingSchema[])

Creates mapping schema, derived from other mapping schemata.

public MappingSchema(params MappingSchema[] schemas)

Parameters

schemas MappingSchema[]

Base mapping schemata.

MappingSchema(string?)

Creates mapping schema for specified configuration name.

public MappingSchema(string? configuration)

Parameters

configuration string

Mapping schema configuration name. ProviderName for standard names.

Remarks

Schema name should be unique for mapping schemas with different mappings. Using same name could lead to incorrect mapping used when mapping schemas with same name define different mappings for same type.

MappingSchema(string?, params MappingSchema[]?)

Creates mapping schema with specified configuration name and base mapping schemas.

public MappingSchema(string? configuration, params MappingSchema[]? schemas)

Parameters

configuration string

Mapping schema configuration name. ProviderName for standard names.

schemas MappingSchema[]

Base mapping schemas.

Remarks

Schema name should be unique for mapping schemas with different mappings. Using same name could lead to incorrect mapping used when mapping schemas with same name define different mappings for same type.

Fields

Default

Default mapping schema, used by LINQ to DB, when more specific mapping schema not provided.

public static MappingSchema Default

Field Value

MappingSchema

Properties

ColumnNameComparer

Gets or sets column name comparison rules for comparison of column names in mapping with column name, returned by provider's data reader.

public StringComparer ColumnNameComparer { get; set; }

Property Value

StringComparer

ConfigurationList

Gets configurations, associated with current mapping schema.

public string[] ConfigurationList { get; }

Property Value

string[]

DisplayID

public string DisplayID { get; }

Property Value

string

EntityDescriptorCreatedCallback

Gets or sets application-wide action, called when the EntityDescriptor is created. Could be used to adjust created descriptor before use. Not called, when connection has connection-level callback defined (OnEntityDescriptorCreated).

public static Action<MappingSchema, IEntityChangeDescriptor>? EntityDescriptorCreatedCallback { get; set; }

Property Value

Action<MappingSchema, IEntityChangeDescriptor>

IsLockable

public virtual bool IsLockable { get; }

Property Value

bool

IsLocked

public virtual bool IsLocked { get; }

Property Value

bool

ValueToSqlConverter

Gets value to SQL (usually literal) converter.

public ValueToSqlConverter ValueToSqlConverter { get; }

Property Value

ValueToSqlConverter

Methods

AddMetadataReader(IMetadataReader)

Adds additional metadata attributes provider to current schema.

public void AddMetadataReader(IMetadataReader reader)

Parameters

reader IMetadataReader

Metadata attributes provider.

AddScalarType(Type, DataType, bool)

Configure provided type mapping to scalar database type.

public void AddScalarType(Type type, DataType dataType = DataType.Undefined, bool withNullable = true)

Parameters

type Type

Type to configure.

dataType DataType

Optional scalar data type.

withNullable bool

Also register Nullable<T> type.

AddScalarType(Type, SqlDataType, bool)

Configure provided type mapping to scalar database type.

public void AddScalarType(Type type, SqlDataType dataType, bool withNullable = true)

Parameters

type Type

Type to configure.

dataType SqlDataType

Database data type.

withNullable bool

Also register Nullable<T> type.

AddScalarType(Type, object?, DataType)

Configure provided type mapping to scalar database type.

public void AddScalarType(Type type, object? defaultValue, DataType dataType = DataType.Undefined)

Parameters

type Type

Type to configure.

defaultValue object

Default value. See SetDefaultValue(Type, object?) for more details.

dataType DataType

Optional scalar data type.

AddScalarType(Type, object?, bool, DataType)

Configure provided type mapping to scalar database type.

public void AddScalarType(Type type, object? defaultValue, bool canBeNull, DataType dataType = DataType.Undefined)

Parameters

type Type

Type to configure.

defaultValue object

Default value. See SetDefaultValue(Type, object?) for more details.

canBeNull bool

Set null value support flag. See SetCanBeNull(Type, bool) for more details.

dataType DataType

Optional scalar data type.

ChangeType(object?, Type)

Converts value to specified type.

public object? ChangeType(object? value, Type conversionType)

Parameters

value object

Value to convert.

conversionType Type

Target type.

Returns

object

Converted value.

ChangeTypeTo<T>(object?)

Converts value to specified type.

public T ChangeTypeTo<T>(object? value)

Parameters

value object

Value to convert.

Returns

T

Converted value.

Type Parameters

T

Target type.

ClearCache()

Clears EntityDescriptor cache.

public static void ClearCache()

CombineSchemas(MappingSchema, MappingSchema)

Internal API.

public static MappingSchema CombineSchemas(MappingSchema ms1, MappingSchema ms2)

Parameters

ms1 MappingSchema
ms2 MappingSchema

Returns

MappingSchema

EnumToValue(Enum)

Converts enum value to database value.

public object? EnumToValue(Enum value)

Parameters

value Enum

Enum value.

Returns

object

Database value.

GenerateConvertedValueExpression(object?, Type)

public Expression GenerateConvertedValueExpression(object? value, Type type)

Parameters

value object
type Type

Returns

Expression

GenerateID()

protected virtual int GenerateID()

Returns

int

GenerateSafeConvert(Type, Type)

public LambdaExpression GenerateSafeConvert(Type fromType, Type type)

Parameters

fromType Type
type Type

Returns

LambdaExpression

GetAttribute<T>(Type)

Gets attribute of specified type, associated with specified type. Attributes are filtered by schema's configuration names (see ConfigurationList).

public T? GetAttribute<T>(Type type) where T : MappingAttribute

Parameters

type Type

Attribute owner type.

Returns

T

First found attribute of specified type or null, if no attributes found.

Type Parameters

T

Mapping attribute type (must inherit MappingAttribute).

GetAttribute<T>(Type, MemberInfo)

Gets attribute of specified type, associated with specified type member. Attributes are filtered by schema's configuration names (see ConfigurationList).

public T? GetAttribute<T>(Type type, MemberInfo memberInfo) where T : MappingAttribute

Parameters

type Type

Member's owner type.

memberInfo MemberInfo

Attribute owner member.

Returns

T

First found attribute of specified type or null, if no attributes found.

Type Parameters

T

Mapping attribute type (must inherit MappingAttribute).

GetAttributes<T>(Type)

Gets attributes of specified type, associated with specified type. Attributes are filtered by schema's configuration names (see ConfigurationList).

public T[] GetAttributes<T>(Type type) where T : MappingAttribute

Parameters

type Type

Attributes owner type.

Returns

T[]

Attributes of specified type.

Type Parameters

T

Mapping attribute type (must inherit MappingAttribute).

GetAttributes<T>(Type, MemberInfo, bool)

Gets attributes of specified type, associated with specified type member. Attributes are filtered by schema's configuration names (see ConfigurationList).

public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool forFirstConfiguration = false) where T : MappingAttribute

Parameters

type Type

Member's owner type.

memberInfo MemberInfo

Attributes owner member.

forFirstConfiguration bool

If true - returns only atributes for first configuration with attributes from ConfigurationList.

Returns

T[]

Attributes of specified type.

Type Parameters

T

Mapping attribute type (must inherit MappingAttribute).

GetCanBeNull(Type)

Returns true, if value of specified type could contain null.

public bool GetCanBeNull(Type type)

Parameters

type Type

Value type.

Returns

bool

Returns true if specified type supports null values.

GetConvertExpression(DbDataType, DbDataType, bool, bool, ConversionType)

Returns conversion expression from from type to to type.

public LambdaExpression? GetConvertExpression(DbDataType from, DbDataType to, bool checkNull = true, bool createDefault = true, ConversionType conversionType = ConversionType.Common)

Parameters

from DbDataType

Source type.

to DbDataType

Target type.

checkNull bool

If true, and source type could contain null, conversion expression will check converted value for null and replace it with default value. SetDefaultValue(Type, object?) for more details.

createDefault bool

Create new conversion expression, if conversion is not defined.

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

LambdaExpression

Conversion expression or null, if there is no such conversion and createDefault is false.

GetConvertExpression(Type, Type, bool, bool, ConversionType)

Returns conversion expression from from type to to type.

public LambdaExpression? GetConvertExpression(Type from, Type to, bool checkNull = true, bool createDefault = true, ConversionType conversionType = ConversionType.Common)

Parameters

from Type

Source type.

to Type

Target type.

checkNull bool

If true, and source type could contain null, conversion expression will check converted value for null and replace it with default value. SetDefaultValue(Type, object?) for more details.

createDefault bool

Create new conversion expression, if conversion is not defined.

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

LambdaExpression

Conversion expression or null, if there is no such conversion and createDefault is false.

GetConvertExpression<TFrom, TTo>(bool, bool, ConversionType)

Returns conversion expression from TFrom type to TTo type.

public Expression<Func<TFrom, TTo>>? GetConvertExpression<TFrom, TTo>(bool checkNull = true, bool createDefault = true, ConversionType conversionType = ConversionType.Common)

Parameters

checkNull bool

If true, and source type could contain null, conversion expression will check converted value for null and replace it with default value. SetDefaultValue(Type, object?) for more details.

createDefault bool

Create new conversion expression, if conversion is not defined.

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

Expression<Func<TFrom, TTo>>

Conversion expression or null, if there is no such conversion and createDefault is false.

Type Parameters

TFrom

Source type.

TTo

Target type.

GetConverter<TFrom, TTo>(ConversionType)

Returns conversion delegate for conversion from TFrom type to TTo type.

public Func<TFrom, TTo>? GetConverter<TFrom, TTo>(ConversionType conversionType = ConversionType.Common)

Parameters

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

Func<TFrom, TTo>

Conversion delegate or null if conversion is not defined.

Type Parameters

TFrom

Source type.

TTo

Target type.

GetDataType(Type)

Returns database type mapping information for specified type.

public SqlDataType GetDataType(Type type)

Parameters

type Type

Mapped type.

Returns

SqlDataType

Database type information.

GetDefaultFromEnumType(Type)

Returns type, to which provided enumeration type is mapped or null, if type is not configured. See SetDefaultFromEnumType(Type, Type).

public Type? GetDefaultFromEnumType(Type enumType)

Parameters

enumType Type

Enumeration type.

Returns

Type

Mapped type or null.

GetDefaultValue(Type)

Returns default value for specified type. Default value is a value, used instead of NULL value, read from database.

public object? GetDefaultValue(Type type)

Parameters

type Type

Value type.

Returns

object

Returns default value for type.

GetDefinedTypes()

Enumerates types registered by FluentMappingBuilder.

public IEnumerable<Type> GetDefinedTypes()

Returns

IEnumerable<Type>

Returns all types, mapped by fluent mappings.

GetDynamicColumns(Type)

Gets the dynamic columns defined on given type.

public MemberInfo[] GetDynamicColumns(Type type)

Parameters

type Type

The type.

Returns

MemberInfo[]

All dynamic columns defined on given type.

GetEntityDescriptor(Type, Action<MappingSchema, IEntityChangeDescriptor>?)

Returns mapped entity descriptor.

public EntityDescriptor GetEntityDescriptor(Type type, Action<MappingSchema, IEntityChangeDescriptor>? onEntityDescriptorCreated = null)

Parameters

type Type

Mapped type.

onEntityDescriptorCreated Action<MappingSchema, IEntityChangeDescriptor>

Action, called when new descriptor instance created. When set to null, EntityDescriptorCreatedCallback callback used.

Returns

EntityDescriptor

Mapping descriptor.

GetMapValues(Type)

Returns enum type mapping information or null for non-enum types.

public virtual MapValue[]? GetMapValues(Type type)

Parameters

type Type

Mapped type.

Returns

MapValue[]

Mapping values for enum type and null for non-enum types.

GetUnderlyingDataType(Type, out bool)

Returns scalar database type mapping information for provided type.

public SqlDataType GetUnderlyingDataType(Type type, out bool canBeNull)

Parameters

type Type

Mapped type.

canBeNull bool

Returns true, if type type is enum with mapping to null value. Initial parameter value, passed to this method is not used.

Returns

SqlDataType

Scalar database type information.

HasAttribute<T>(Type)

Returns true if attribute of specified type, associated with specified type. Attributes are filtered by schema's configuration names (see ConfigurationList).

public bool HasAttribute<T>(Type type) where T : MappingAttribute

Parameters

type Type

Attribute owner type.

Returns

bool

Returns true if attribute of specified type, associated with specified type.

Type Parameters

T

Mapping attribute type (must inherit MappingAttribute).

HasAttribute<T>(Type, MemberInfo)

Returns true if attribute of specified type, associated with specified type member. Attributes are filtered by schema's configuration names (see ConfigurationList).

public bool HasAttribute<T>(Type type, MemberInfo memberInfo) where T : MappingAttribute

Parameters

type Type

Member's owner type.

memberInfo MemberInfo

Attribute owner member.

Returns

bool

Returns true if attribute of specified type, associated with specified type member.

Type Parameters

T

Mapping attribute type (must inherit MappingAttribute).

InitGenericConvertProvider(params Type[])

Initialize generic conversions for specific type parameters.

public bool InitGenericConvertProvider(params Type[] types)

Parameters

types Type[]

Generic type parameters.

Returns

bool

Returns true if new generic type conversions could have added to mapping schema.

InitGenericConvertProvider<T>()

Initialize generic conversions for specific type parameter.

public void InitGenericConvertProvider<T>()

Type Parameters

T

Generic type parameter, for which converters should be initialized.

IsScalarType(Type)

Returns true, if provided type mapped to scalar database type in current schema.

public bool IsScalarType(Type type)

Parameters

type Type

Type to check.

Returns

bool

true, if type mapped to scalar database type.

SetCanBeNull(Type, bool)

Sets null value support flag for specified type.

public void SetCanBeNull(Type type, bool value)

Parameters

type Type

Value type.

value bool

If true, specified type value could contain null.

SetConvertExpression(DbDataType, DbDataType, LambdaExpression, bool, ConversionType)

Specify conversion expression for conversion from fromType type to toType type.

public MappingSchema SetConvertExpression(DbDataType fromType, DbDataType toType, LambdaExpression expr, bool addNullCheck = true, ConversionType conversionType = ConversionType.Common)

Parameters

fromType DbDataType

Source type.

toType DbDataType

Target type.

expr LambdaExpression

Conversion expression.

addNullCheck bool

If true, conversion expression will be wrapped with default value substitution logic for null values. Wrapper will be added only if source type can have null values and conversion expression doesn't use default value provider. See DefaultValue<T> and DefaultValue types for more details.

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

MappingSchema

SetConvertExpression(Type, Type, LambdaExpression, bool, ConversionType)

Specify conversion expression for conversion from fromType type to toType type.

public MappingSchema SetConvertExpression(Type fromType, Type toType, LambdaExpression expr, bool addNullCheck = true, ConversionType conversionType = ConversionType.Common)

Parameters

fromType Type

Source type.

toType Type

Target type.

expr LambdaExpression

Conversion expression.

addNullCheck bool

If true, conversion expression will be wrapped with default value substitution logic for null values. Wrapper will be added only if source type can have null values and conversion expression doesn't use default value provider. See DefaultValue<T> and DefaultValue types for more details.

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

MappingSchema

SetConvertExpression<TFrom, TTo>(Expression<Func<TFrom, TTo>>, bool, ConversionType)

Specify conversion expression for conversion from TFrom type to TTo type.

public MappingSchema SetConvertExpression<TFrom, TTo>(Expression<Func<TFrom, TTo>> expr, bool addNullCheck = true, ConversionType conversionType = ConversionType.Common)

Parameters

expr Expression<Func<TFrom, TTo>>

Conversion expression.

addNullCheck bool

If true, conversion expression will be wrapped with default value substitution logic for null values. Wrapper will be added only if source type can have null values and conversion expression doesn't use default value provider. See DefaultValue<T> and DefaultValue types for more details.

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

MappingSchema

Type Parameters

TFrom

Source type.

TTo

Target type.

SetConvertExpression<TFrom, TTo>(Expression<Func<TFrom, TTo>>, Expression<Func<TFrom, TTo>>, ConversionType)

Specify conversion expression for conversion from TFrom type to TTo type.

public MappingSchema SetConvertExpression<TFrom, TTo>(Expression<Func<TFrom, TTo>> checkNullExpr, Expression<Func<TFrom, TTo>> expr, ConversionType conversionType = ConversionType.Common)

Parameters

checkNullExpr Expression<Func<TFrom, TTo>>

null values conversion expression.

expr Expression<Func<TFrom, TTo>>

Conversion expression.

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

MappingSchema

Type Parameters

TFrom

Source type.

TTo

Target type.

SetConverter<TFrom, TTo>(Func<TFrom, TTo>, ConversionType)

Specify conversion delegate for conversion from TFrom type to TTo type.

public MappingSchema SetConverter<TFrom, TTo>(Func<TFrom, TTo> func, ConversionType conversionType = ConversionType.Common)

Parameters

func Func<TFrom, TTo>

Conversion delegate.

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

MappingSchema

Type Parameters

TFrom

Source type.

TTo

Target type.

SetConverter<TFrom, TTo>(Func<TFrom, TTo>, DbDataType, DbDataType, ConversionType)

Specify conversion delegate for conversion from TFrom type to TTo type.

public MappingSchema SetConverter<TFrom, TTo>(Func<TFrom, TTo> func, DbDataType from, DbDataType to, ConversionType conversionType = ConversionType.Common)

Parameters

func Func<TFrom, TTo>

Conversion delegate.

from DbDataType

Source type detalization

to DbDataType

Target type detalization

conversionType ConversionType

Conversion type. See ConversionType for more details.

Returns

MappingSchema

Type Parameters

TFrom

Source type.

TTo

Target type.

SetCultureInfo(CultureInfo)

Set conversion expressions for conversion from and to string for basic types (byte, sbyte, short, ushort, int, uint, long, ulong , float, double, decimal, DateTime, DateTimeOffset) using provided culture format providers.

public void SetCultureInfo(CultureInfo info)

Parameters

info CultureInfo

Culture with format providers for conversions.

SetDataType(Type, DataType)

Associate specified type with LINQ to DB data type.

public void SetDataType(Type type, DataType dataType)

Parameters

type Type

Mapped type.

dataType DataType

LINQ to DB data type.

SetDataType(Type, SqlDataType)

Associate specified type with database data type.

public void SetDataType(Type type, SqlDataType dataType)

Parameters

type Type

Mapped type.

dataType SqlDataType

Database data type.

SetDefaultFromEnumType(Type, Type)

Sets type, to which provided enumeration type should be mapped.

public void SetDefaultFromEnumType(Type enumType, Type defaultFromType)

Parameters

enumType Type

Enumeration type.

defaultFromType Type

Mapped type.

SetDefaultValue(Type, object?)

Sets default value for specific type. Default value is a value, used instead of NULL value, read from database.

public void SetDefaultValue(Type type, object? value)

Parameters

type Type

Value type.

value object

Default value.

SetGenericConvertProvider(Type)

Adds generic type conversions provider. Type converter must implement IGenericInfoProvider interface. IGenericInfoProvider for more details and examples.

public void SetGenericConvertProvider(Type type)

Parameters

type Type

Generic type conversions provider.

SetScalarType(Type, bool)

Configure how provided type should be handled during mapping to database - as scalar value or composite type.

public void SetScalarType(Type type, bool isScalarType = true)

Parameters

type Type

Type to configure.

isScalarType bool

true, if provided type should be mapped to scalar database value.

SetValueToSqlConverter(Type, Action<StringBuilder, SqlDataType, DataOptions, object>)

Sets value to SQL converter action for specific value type.

public MappingSchema SetValueToSqlConverter(Type type, Action<StringBuilder, SqlDataType, DataOptions, object> converter)

Parameters

type Type

Value type.

converter Action<StringBuilder, SqlDataType, DataOptions, object>

Converter action. Action accepts three parameters: - SQL string builder to write generated value SQL to; - value SQL type descriptor; - value.

Returns

MappingSchema

SetValueToSqlConverter(Type, Action<StringBuilder, SqlDataType, object>)

Sets value to SQL converter action for specific value type.

public MappingSchema SetValueToSqlConverter(Type type, Action<StringBuilder, SqlDataType, object> converter)

Parameters

type Type

Value type.

converter Action<StringBuilder, SqlDataType, object>

Converter action. Action accepts three parameters: - SQL string builder to write generated value SQL to; - value SQL type descriptor; - value.

Returns

MappingSchema

TryGetConvertExpression(Type, Type)

Returns custom value conversion expression from from type to to type if it is defined in mapping schema, or null otherwise.

public virtual LambdaExpression? TryGetConvertExpression(Type from, Type to)

Parameters

from Type

Source type.

to Type

Target type.

Returns

LambdaExpression

Conversion expression or null, if conversion is not defined.