ConversionType
This enum sets the ConversionType property of the Converter object. It controls how a DbDataReader is converted into its equivalent .NET CLR type.
Enum Values
| Name | Description |
|---|---|
| Default | The conversion is strict. No additional implied logic is applied during the conversion of the DbDataReader object into its destination .NET CLR type. |
| Automatic | The conversion is non-strict (automatic). Additional logic from System.Linq.Expressions.Expression.Convert(Expression, Type) and/or System.Convert is applied to map the DbDataReader object into its destination .NET CLR type. The operation is compiled ahead-of-time (AOT) and will only succeed if both data types are convertible. |
Use-Cases
Use Automatic when the database column type differs from the class property type (e.g., Int to string, string to Guid, System.DateTime to string, etc).
Given the following class:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
The library will succeed when the table is defined as:
CREATE TABLE [dbo].[Customer]
{
[Id] INT IDENTITY(1, 1) NOT NULL,
[Name] NVARCHAR(32) NOT NULL,
[Age] INT NOT NULL
}
However, the library will fail when the table is defined as:
CREATE TABLE [dbo].[Customer]
{
[Id] INT IDENTITY(1, 1) NOT NULL,
[Name] NVARCHAR(32) NOT NULL,
[Age] NVARCHAR(8) NOT NULL -- There is no explicit converter between STRING and INT
}
Usability
Set the ConversionType property on the Converter object.
Converter.ConversionType = ConversionType.Automatic;
Please be reminded that if the database column is not auto-convertible to a target property, then the
System.Convertmethod will used during the transformation. With this additional logic implied, the performance will be affected during the transformations.