动态类型转换 Entity Framework ORM
Dynamic typeConvertion Entity Framework ORM
您好,我正在处理使用 Telerik ORM 的遗留代码。
基于从后端 db2 迁移的 MSsql 数据库。
据我所知,DB2 不提供用于在表中存储 布尔值 的专用类型。此问题有不同的解决方法。在我的例子中,决定使用 custom Sql type BITTYPE:char(1)
,这实际上保留了两个不同的 char(1)值('1' (true)
和'0' (false)
)。
Telerik ORM 使用自定义 AdoTypeConverter
将此 char(1) 值转换为域 布尔属性 .
public class BitTypeToBooleanConverter : TypeConverterBase
{
public BitTypeToBooleanConverter()
{
this.ConverterType = typeof(bool);
this.NullableConverterType = typeof(bool?);
}
public override object Read(ref DataHolder holder)
{
bool n = holder.Reader.IsDBNull(holder.Position);
holder.NoValue = n;
if (n)
{
if (nullable)
holder.ObjectValue = null;
else if (holder.Box)
holder.ObjectValue = false;
else
holder.BooleanValue = false;
}
else
{
string s = holder.Reader.GetValue(holder.Position).ToString();
bool outValue = false;
if (!bool.TryParse(s,out outValue))
{
if (s.Equals("1"))
{
outValue = true;
}
}
if (nullable || holder.Box)
holder.ObjectValue = outValue;
else
holder.BooleanValue = outValue;
}
return (holder.Box || nullable) ? holder.ObjectValue : null;
}
public override void Write(ref DataHolder holder)
{
holder.Parameter.DbType = System.Data.DbType.String;
if (holder.NoValue)
{
holder.Parameter.Size = 1;
holder.Parameter.Value = null;
}
else
{
string s = (holder.BooleanValue ? "1" : "0");
holder.Parameter.Size = s.Length;
holder.Parameter.Value = s;
}
}
public override bool CreateLiteralSql(ref DataHolder holder)
{
if (holder.NoValue)
{
holder.StringValue = "NULL";
return false;
}
else
{
holder.StringValue = (holder.BooleanValue ? "1" : "0");
return true; // inidicating that ' are needed around, because it is a character column (VARCHAR)
}
}
问: 我不能更改数据库,但我需要以某种方式教 Entity Framework ORM 适用于相同的场景
P.s看完文章后(
我尝试使用约定来解决这个问题,但没有带来结果。
- BITTYPE:char(1) - 错误: 在 Sql 服务器提供商清单中找不到
- Char - **错误:**指定的架构无效。错误:
(8,12):错误 2019:指定的成员映射无效。 ..
代码片段:
public class BitTypeCharConvention : Convention
{
private const string DataType = "BITTYPE:char(1)";
public BitTypeCharConvention()
{
Properties<bool>().Configure(c => c.HasColumnType(DataType));
}
}
我也知道我可以尝试创建另一个 属性 来转换字符串的值,但我想要更多 "reusable" 变体
结论:经过一番搜索,我还真没有找到适合解决额头问题的方法。因此决定在数据访问级别保持默认 Entity Framework 行为(不进行任何转换),并通过映射(自动映射器)(即实体到域模型)在下一个应用程序级别完成这项工作
您好,我正在处理使用 Telerik ORM 的遗留代码。 基于从后端 db2 迁移的 MSsql 数据库。
据我所知,DB2 不提供用于在表中存储 布尔值 的专用类型。此问题有不同的解决方法。在我的例子中,决定使用 custom Sql type BITTYPE:char(1)
,这实际上保留了两个不同的 char(1)值('1' (true)
和'0' (false)
)。
Telerik ORM 使用自定义 AdoTypeConverter
将此 char(1) 值转换为域 布尔属性 .
public class BitTypeToBooleanConverter : TypeConverterBase
{
public BitTypeToBooleanConverter()
{
this.ConverterType = typeof(bool);
this.NullableConverterType = typeof(bool?);
}
public override object Read(ref DataHolder holder)
{
bool n = holder.Reader.IsDBNull(holder.Position);
holder.NoValue = n;
if (n)
{
if (nullable)
holder.ObjectValue = null;
else if (holder.Box)
holder.ObjectValue = false;
else
holder.BooleanValue = false;
}
else
{
string s = holder.Reader.GetValue(holder.Position).ToString();
bool outValue = false;
if (!bool.TryParse(s,out outValue))
{
if (s.Equals("1"))
{
outValue = true;
}
}
if (nullable || holder.Box)
holder.ObjectValue = outValue;
else
holder.BooleanValue = outValue;
}
return (holder.Box || nullable) ? holder.ObjectValue : null;
}
public override void Write(ref DataHolder holder)
{
holder.Parameter.DbType = System.Data.DbType.String;
if (holder.NoValue)
{
holder.Parameter.Size = 1;
holder.Parameter.Value = null;
}
else
{
string s = (holder.BooleanValue ? "1" : "0");
holder.Parameter.Size = s.Length;
holder.Parameter.Value = s;
}
}
public override bool CreateLiteralSql(ref DataHolder holder)
{
if (holder.NoValue)
{
holder.StringValue = "NULL";
return false;
}
else
{
holder.StringValue = (holder.BooleanValue ? "1" : "0");
return true; // inidicating that ' are needed around, because it is a character column (VARCHAR)
}
}
问: 我不能更改数据库,但我需要以某种方式教 Entity Framework ORM 适用于相同的场景
P.s看完文章后(
我尝试使用约定来解决这个问题,但没有带来结果。
- BITTYPE:char(1) - 错误: 在 Sql 服务器提供商清单中找不到
- Char - **错误:**指定的架构无效。错误: (8,12):错误 2019:指定的成员映射无效。 ..
代码片段:
public class BitTypeCharConvention : Convention
{
private const string DataType = "BITTYPE:char(1)";
public BitTypeCharConvention()
{
Properties<bool>().Configure(c => c.HasColumnType(DataType));
}
}
我也知道我可以尝试创建另一个 属性 来转换字符串的值,但我想要更多 "reusable" 变体
结论:经过一番搜索,我还真没有找到适合解决额头问题的方法。因此决定在数据访问级别保持默认 Entity Framework 行为(不进行任何转换),并通过映射(自动映射器)(即实体到域模型)在下一个应用程序级别完成这项工作