是否有既定的方法/工具来根据Oracle DB表的列自动创建包含变量的生成C#class/struct?
我目前正在使用ODP.Net来处理Oracle DB中的许多表.为了方便我处理数据库表,我为每个我创建的表创建了一个独特的C#类,它存储了给定DB行的信息
目前,我手动制作每个班级.但我意识到随着桌子数量的增加,我的方式变得更加乏味.
任何人遇到同样的问题,有一个很好的工具来缓解任务?
在一般意义上,您所说的是将数据库的模式/元数据转换为不同的形式.大多数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;
您将注意到第一列,将其中一些字段连接在一起以形成类属性:
在过去,我添加了列来生成从OracleCommand参数到Web输入的所有内容.然后,您可以采取进一步用某种模板机制生成整个类,包括数据访问方法等T4是一个选项,但我用的CodeSmith的发电机产品.
虽然像Entities或NHibernate这样的对象关系映射器也做了很多这一代,我不理解他们的其他目的,即抽象数据从数据库到中间层的实际移动 - 他们使用的查询语言也是如此对我来说很脆弱/错综复杂.我更愿意坚持使用SQL本身,也许微ORM喜欢短小精悍的 - 当然我用的是相同的技术上面生成CRUD操作的SQL以及:).