使用 ODP.NET,从 Oracle 数据库 Table 的列信息创建 C# class/struct
With ODP.NET, Create C# class/struct from Column Info of an Oracle DB's Table
是否已建立 way/tool 来自动创建包含根据 Oracle DB Table 的列的变量的生成 C# class/struct?
我目前正在使用 ODP.Net 处理来自 Oracle 数据库的多个 Table。为了方便我处理数据库 Table,我为我读取的每个 Table 创建了一个唯一的 C# class,它存储给定数据库行的信息
目前,我正在手动制作每个 class。但是我意识到随着桌子的数量越来越多,我的方式变得越来越乏味。
有人遇到同样的问题,有什么好的工具可以简化任务吗?
Microsoft 介绍 Code Generation and T4 Text Templates which can be used to generate text output using some control logic. This article 描述了一种如何从数据库自动生成代码的方法。 T4 是 Visual Studio 的一部分,因此无需安装其他软件。要完成任务,您需要编写
的 T4 文本模板
a mixture of text blocks and control logic that can generate a text
file. The control logic is written as fragments of program code in
Visual C# or Visual Basic.
您还可以采用 this article where it's shown how to create classes from the Datatable
object. Basically you can use the System.CodeDom
命名空间中描述的方法来创建包含您需要的属性的 class。为了解决问题,您需要完成以下步骤:
- 使用
CodeTypeDeclaration
class. 声明一个 class
- 为每一列创建属性 (
CodeMemberField
) 创建一个 属性 并将其添加到 class.
- 创建命名空间 (
CodeNamespace
) 并将 class 添加到其中。
- 使用
CSharpCodeProvider
class. 生成一个 .cs
文件
我认为第一种方法(使用T4)更有效也更容易理解。例如,假设您对所有 classes 使用相同的方法。你可以简单地将这个方法添加到一个T4文件中,这个方法是做什么的就一目了然了。第二种方法需要更多代码来定义方法。
UDPATE
NHibernate Mapping Generator 允许从数据库生成域代码。您可以简单地复制这些生成的 classes(从 Domain Code
选项卡)并更改它们。
如果您只需要为您的 class 生成属性,那么您可以为 Oracle 改编 SQL 描述的脚本 here。
Entity Framework 与 ODP.NET 一起工作,并为您做这件事,还有更多...您没有使用任何可用的 ER 映射器是否有特定原因?
在一般意义上,您是在谈论将数据库的 schema/metadata 转换为不同的形式。大多数 RDBMS 将此数据公开为表。例如,这是我用来获取所有列数据、清理它(如删除下划线)并将其转换为 .net 类型和语句的查询:
with metadata as
(
select
utc.table_name,
utc.column_name,
replace(initcap(replace( lower(utc.column_name) ,'_',' ')),' ','') as column_name_clean,
initcap(replace( lower(utc.column_name) ,'_',' ')) as column_name_space,
rtrim(substr(utc.column_name,1,26),'_') as column_name_26,
case utc.data_type
when 'DATE' then 'DateTime'
when 'VARCHAR2' then 'String'
when 'CLOB' then 'String'
when 'NUMBER' then
case when utc.data_scale=0 then
case
when utc.data_precision = 19 then 'Int64'
when utc.data_precision = 9 then 'Int32'
when utc.data_precision = 4 then 'Int16'
when utc.data_precision = 1 then 'Boolean'
else 'Int'|| utc.data_precision end
else 'Decimal' end
when 'CHAR' then
case when utc.data_length = 1 then 'Char'
else 'String' end
else '' end as clr_data_type,
case utc.data_type
when 'DATE' then 'DateTime'
when 'VARCHAR2' then 'Text'
when 'CLOB' then 'MultilineText'
when 'CHAR' then 'Text'
else '' end as mvc_data_type,
case utc.data_type
when 'DATE' then 'Date'
when 'TIMESTAMP' then 'TimeStamp'
when 'VARCHAR2' then 'Varchar2'
when 'CLOB' then 'Clob'
when 'NUMBER' then
case when utc.data_scale=0 then
case
when utc.data_precision = 19 then 'Int64'
when utc.data_precision = 9 then 'Int32'
when utc.data_precision = 4 then 'Int16'
when utc.data_precision = 1 then 'Decimal-Boolean'
else 'Int'|| utc.data_precision end
else 'Decimal' end
when 'CHAR' then 'Char'
else '' end as odp_data_type,
utc.Data_Type as native_data_type,
case when utc.data_type = 'VARCHAR2' or utc.data_type='CHAR' then Data_Length
else null end as mvc_data_range,
utc.data_length,
utc.data_precision,
utc.data_scale,
utc.nullable,
case
when utc.data_scale > 0 then
'^\d{' || (nvl(utc.data_precision,1)-nvl(utc.data_scale,0)) || '}(\.\d{' || nvl(utc.data_scale,0) || '})?$'
else '' end as validation_reg_ex,
'n.' || trim(rpad(' ', nvl(utc.data_scale,0), 'd')) as validation_format,
case utc.data_type
when 'NUMBER' then
case when utc.data_scale=0 then utc.data_precision
else utc.data_precision + 1 end -- +1 for the decimal
else
utc.data_length end as max_string_length,
case ac.constraint_type when 'P' then 'Y' else 'N' end as PRIMARY_KEY,
ac.constraint_type
from user_tab_columns utc
left join all_cons_columns acc
join all_constraints ac on ac.constraint_name = acc.constraint_name and ac.owner = acc.owner and ac.constraint_type='P'
on utc.column_name=acc.column_name and utc.table_name=acc.table_name
where utc.table_name not like 'BIN%'
order by utc.table_name, utc.column_id
)
select 'public ' || clr_data_type || ' ' || column_name_clean || ' {get; set;}' as ClassProperty, m.*
from metadata m;
您会注意到第一列,将其中一些字段连接在一起形成 class 属性:
过去,我添加了列来生成从 OracleCommand 参数到 Web 输入的所有内容。然后,您可以使用某种模板机制更进一步,生成整个 classes,包括数据访问方法等。T4 是一个选项,但我使用了 CodeSmith 的生成器产品。
虽然像 Entities 或 NHibernate 这样的对象关系映射器也在这一代做了很多工作,但我不欣赏它们的其他目的,即抽象数据从数据库到中间层的实际移动 - 查询语言他们使用的对我来说太 weak/convoluted 了。我更喜欢坚持 sql 本身,也许还有像 dapper 这样的微型 orm - 当然我也使用上面相同的技术为 CRUD 操作生成 sql :).
是否已建立 way/tool 来自动创建包含根据 Oracle DB Table 的列的变量的生成 C# class/struct?
我目前正在使用 ODP.Net 处理来自 Oracle 数据库的多个 Table。为了方便我处理数据库 Table,我为我读取的每个 Table 创建了一个唯一的 C# class,它存储给定数据库行的信息
目前,我正在手动制作每个 class。但是我意识到随着桌子的数量越来越多,我的方式变得越来越乏味。
有人遇到同样的问题,有什么好的工具可以简化任务吗?
Microsoft 介绍 Code Generation and T4 Text Templates which can be used to generate text output using some control logic. This article 描述了一种如何从数据库自动生成代码的方法。 T4 是 Visual Studio 的一部分,因此无需安装其他软件。要完成任务,您需要编写
的 T4 文本模板a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic.
您还可以采用 this article where it's shown how to create classes from the Datatable
object. Basically you can use the System.CodeDom
命名空间中描述的方法来创建包含您需要的属性的 class。为了解决问题,您需要完成以下步骤:
- 使用
CodeTypeDeclaration
class. 声明一个 class
- 为每一列创建属性 (
CodeMemberField
) 创建一个 属性 并将其添加到 class. - 创建命名空间 (
CodeNamespace
) 并将 class 添加到其中。 - 使用
CSharpCodeProvider
class. 生成一个
.cs
文件
我认为第一种方法(使用T4)更有效也更容易理解。例如,假设您对所有 classes 使用相同的方法。你可以简单地将这个方法添加到一个T4文件中,这个方法是做什么的就一目了然了。第二种方法需要更多代码来定义方法。
UDPATE
NHibernate Mapping Generator 允许从数据库生成域代码。您可以简单地复制这些生成的 classes(从 Domain Code
选项卡)并更改它们。
如果您只需要为您的 class 生成属性,那么您可以为 Oracle 改编 SQL 描述的脚本 here。
Entity Framework 与 ODP.NET 一起工作,并为您做这件事,还有更多...您没有使用任何可用的 ER 映射器是否有特定原因?
在一般意义上,您是在谈论将数据库的 schema/metadata 转换为不同的形式。大多数 RDBMS 将此数据公开为表。例如,这是我用来获取所有列数据、清理它(如删除下划线)并将其转换为 .net 类型和语句的查询:
with metadata as
(
select
utc.table_name,
utc.column_name,
replace(initcap(replace( lower(utc.column_name) ,'_',' ')),' ','') as column_name_clean,
initcap(replace( lower(utc.column_name) ,'_',' ')) as column_name_space,
rtrim(substr(utc.column_name,1,26),'_') as column_name_26,
case utc.data_type
when 'DATE' then 'DateTime'
when 'VARCHAR2' then 'String'
when 'CLOB' then 'String'
when 'NUMBER' then
case when utc.data_scale=0 then
case
when utc.data_precision = 19 then 'Int64'
when utc.data_precision = 9 then 'Int32'
when utc.data_precision = 4 then 'Int16'
when utc.data_precision = 1 then 'Boolean'
else 'Int'|| utc.data_precision end
else 'Decimal' end
when 'CHAR' then
case when utc.data_length = 1 then 'Char'
else 'String' end
else '' end as clr_data_type,
case utc.data_type
when 'DATE' then 'DateTime'
when 'VARCHAR2' then 'Text'
when 'CLOB' then 'MultilineText'
when 'CHAR' then 'Text'
else '' end as mvc_data_type,
case utc.data_type
when 'DATE' then 'Date'
when 'TIMESTAMP' then 'TimeStamp'
when 'VARCHAR2' then 'Varchar2'
when 'CLOB' then 'Clob'
when 'NUMBER' then
case when utc.data_scale=0 then
case
when utc.data_precision = 19 then 'Int64'
when utc.data_precision = 9 then 'Int32'
when utc.data_precision = 4 then 'Int16'
when utc.data_precision = 1 then 'Decimal-Boolean'
else 'Int'|| utc.data_precision end
else 'Decimal' end
when 'CHAR' then 'Char'
else '' end as odp_data_type,
utc.Data_Type as native_data_type,
case when utc.data_type = 'VARCHAR2' or utc.data_type='CHAR' then Data_Length
else null end as mvc_data_range,
utc.data_length,
utc.data_precision,
utc.data_scale,
utc.nullable,
case
when utc.data_scale > 0 then
'^\d{' || (nvl(utc.data_precision,1)-nvl(utc.data_scale,0)) || '}(\.\d{' || nvl(utc.data_scale,0) || '})?$'
else '' end as validation_reg_ex,
'n.' || trim(rpad(' ', nvl(utc.data_scale,0), 'd')) as validation_format,
case utc.data_type
when 'NUMBER' then
case when utc.data_scale=0 then utc.data_precision
else utc.data_precision + 1 end -- +1 for the decimal
else
utc.data_length end as max_string_length,
case ac.constraint_type when 'P' then 'Y' else 'N' end as PRIMARY_KEY,
ac.constraint_type
from user_tab_columns utc
left join all_cons_columns acc
join all_constraints ac on ac.constraint_name = acc.constraint_name and ac.owner = acc.owner and ac.constraint_type='P'
on utc.column_name=acc.column_name and utc.table_name=acc.table_name
where utc.table_name not like 'BIN%'
order by utc.table_name, utc.column_id
)
select 'public ' || clr_data_type || ' ' || column_name_clean || ' {get; set;}' as ClassProperty, m.*
from metadata m;
您会注意到第一列,将其中一些字段连接在一起形成 class 属性:
过去,我添加了列来生成从 OracleCommand 参数到 Web 输入的所有内容。然后,您可以使用某种模板机制更进一步,生成整个 classes,包括数据访问方法等。T4 是一个选项,但我使用了 CodeSmith 的生成器产品。
虽然像 Entities 或 NHibernate 这样的对象关系映射器也在这一代做了很多工作,但我不欣赏它们的其他目的,即抽象数据从数据库到中间层的实际移动 - 查询语言他们使用的对我来说太 weak/convoluted 了。我更喜欢坚持 sql 本身,也许还有像 dapper 这样的微型 orm - 当然我也使用上面相同的技术为 CRUD 操作生成 sql :).