【发布时间】:2011-12-07 23:00:28
【问题描述】:
.Net 或 Visual Studio 中是否有任何内置功能可以让我基于 MySql 表创建类。我想我说的是坚持。我只希望该类是表的一对一映射。有没有免费的东西?
【问题讨论】:
标签: c# .net mysql database persistence
.Net 或 Visual Studio 中是否有任何内置功能可以让我基于 MySql 表创建类。我想我说的是坚持。我只希望该类是表的一对一映射。有没有免费的东西?
【问题讨论】:
标签: c# .net mysql database persistence
也许你需要这样的东西:
select 'my_table' into @table; #table name
select 'my_database' into @schema; #database name
select concat('public class ',@table,'{') union
select concat('public ',tps.dest,' ',column_name,'{get;set;}') from information_schema.columns c
join( #datatypes mapping
select 'char' as orign ,'string' as dest union all
select 'varchar' ,'string' union all
select 'longtext' ,'string' union all
select 'datetime' ,'DateTime?' union all
select 'text' ,'string' union all
select 'bit' ,'int?' union all
select 'bigint' ,'int?' union all
select 'int' ,'int?' union all
select 'double' ,'double?' union all
select 'decimal' ,'double?' union all
select 'date' ,'DateTime?' union all
select 'tinyint' ,'bool?'
) tps on c.data_type like tps.orign
where table_schema=@schema and table_name=@table union
select '}';
【讨论】:
select concat('public ',tps.dest, IF(tps.dest = 'string', '', IF(is_nullable = 'NO', '', '?')) ... 并从映射中删除 ? 以获得正确的 NULLable 映射
select 'tinytext' ,'string' union all
if(c.IS_NULLABLE = 'NO', REPLACE(tps.dest, '?', '') 而不是tps.dest
我调整了 MeelStorm 的 sql,因为它出现了一些关于语言的错误。我也放入了其他类型的数据,并删除了类声明,因为这对我来说是不必要的。所以最终的结果是:
select concat('public ',tps.dest,' ',column_name,'{get;set;}') as code
from information_schema.columns c
join(
select 'char' as orign ,'string' as dest union all
select 'varchar' ,'string' union all
select 'longtext' ,'string' union all
select 'datetime' ,'DateTime' union all
select 'text' ,'string' union all
select 'bit' ,'int' union all
select 'bigint' ,'int' union all
select 'int' ,'int' union all
select 'double' ,'double' union all
select 'decimal' ,'double' union all
select 'date' ,'DateTime' union all
select 'tinyint' ,'bool'
) tps on c.data_type like tps.orign
where table_schema='your_schema' and table_name='your_table'
order by c.ordinal_position
希望对您有所帮助。干杯!
【讨论】:
这里做得很好:
http://www.code4copy.com/post/generate-c-sharp-model-class-mysql-table
创建一个过程如下:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GenCSharpModel`(in pTableName VARCHAR(255) )
BEGIN
DECLARE vClassName varchar(255);
declare vClassCode mediumtext;
declare v_codeChunk varchar(1024);
DECLARE v_finished INTEGER DEFAULT 0;
DEClARE code_cursor CURSOR FOR
SELECT code FROM temp1;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
set vClassCode ='';
/* Make class name*/
SELECT (CASE WHEN col1 = col2 THEN col1 ELSE concat(col1,col2) END) into vClassName
FROM(
SELECT CONCAT(UCASE(MID(ColumnName1,1,1)),LCASE(MID(ColumnName1,2))) as col1,
CONCAT(UCASE(MID(ColumnName2,1,1)),LCASE(MID(ColumnName2,2))) as col2
FROM
(SELECT SUBSTRING_INDEX(pTableName, '_', -1) as ColumnName2,
SUBSTRING_INDEX(pTableName, '_', 1) as ColumnName1) A) B;
/*store all properties into temp table*/
CREATE TEMPORARY TABLE IF NOT EXISTS temp1 ENGINE=MyISAM
as (
select concat( 'public ', ColumnType , ' ' , FieldName,' { get; set; }') code
FROM(
SELECT (CASE WHEN col1 = col2 THEN col1 ELSE concat(col1,col2) END) AS FieldName,
case DATA_TYPE
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
when 'date' then 'DateTime'
when 'datetime' then 'DateTime'
when 'datetime2' then 'DateTime'
when 'datetimeoffset' then 'DateTimeOffset'
when 'decimal' then 'decimal'
when 'float' then 'float'
when 'image' then 'byte[]'
when 'int' then 'int'
when 'money' then 'decimal'
when 'nchar' then 'char'
when 'ntext' then 'string'
when 'numeric' then 'decimal'
when 'nvarchar' then 'string'
when 'real' then 'double'
when 'smalldatetime' then 'DateTime'
when 'smallint' then 'short'
when 'mediumint' then 'INT'
when 'smallmoney' then 'decimal'
when 'text' then 'string'
when 'time' then 'TimeSpan'
when 'timestamp' then 'DateTime'
when 'tinyint' then 'byte'
when 'uniqueidentifier' then 'Guid'
when 'varbinary' then 'byte[]'
when 'varchar' then 'string'
when 'year' THEN 'UINT'
else 'UNKNOWN_' + DATA_TYPE
end ColumnType
FROM(
select CONCAT(UCASE(MID(ColumnName1,1,1)),LCASE(MID(ColumnName1,2))) as col1,
CONCAT(UCASE(MID(ColumnName2,1,1)),LCASE(MID(ColumnName2,2))) as col2, DATA_TYPE
from
(SELECT SUBSTRING_INDEX(COLUMN_NAME, '_', -1) as ColumnName2,
SUBSTRING_INDEX(COLUMN_NAME, '_', 1) as ColumnName1,
DATA_TYPE, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = pTableName) A) B)C);
set vClassCode = '';
/* concat all properties*/
OPEN code_cursor;
get_code: LOOP
FETCH code_cursor INTO v_codeChunk;
IF v_finished = 1 THEN
LEAVE get_code;
END IF;
-- build code
select CONCAT(vClassCode,'\r\n', v_codeChunk) into vClassCode ;
END LOOP get_code;
CLOSE code_cursor;
drop table temp1;
/*make class*/
select concat('public class ',vClassName,'\r\n{', vClassCode,'\r\n}');
END
不过需要一些手工操作。
【讨论】:
您可以为此使用实体框架。它与 MySQL 连接良好。 我一直在关注这个教程:http://www.devart.com/dotconnect/mysql/articles/tutorial_ef.html
【讨论】:
似乎有一种方法可以让 EntityFramework 与 MySQL 一起工作
【讨论】:
【讨论】:
我使用 NHibernate 和 MyGeneration
MyGeneration 是一个程序,它可以读取您的数据库架构并基于模板生成代码(在 NHibernate 的情况下,实体和映射)
【讨论】:
that will allow my to create classes based off of a MySql table
第一个示例非常好,但缺少一些类型,所以我分享添加缺少的类型(set、float 等..)
select 'table_name' INTO @table; #table name
select 'db_name' into @schema; #database name
select concat('public class ',@table,'{') union
select concat('public ',tps.dest,' ',column_name,'{get;set;}') from information_schema.columns c
join( #datatypes mapping
select 'char' as orign ,'string' as dest union all
select 'varchar' ,'string' union all
select 'longtext' ,'string' union all
select 'datetime' ,'DateTime?' union all
select 'text' ,'string' union all
select 'bit' ,'int?' union all
select 'shorte_prodottoe_prodotto' ,'int?' union all
select 'bigint' ,'int?' union all
select 'float' ,'float' union all
select 'smallint' ,'sbyte' union all
select 'int' ,'int?' union all
select 'double' ,'double?' union all
select 'decimal' ,'double?' union all
select 'date' ,'DateTime?' union all
select 'boolean' ,'bool' union all
select 'set' ,'string' union all
select 'tinyint' ,'bool?'
) tps on c.data_type like tps.orign
where table_schema=@schema and table_name=@table union
select '}';
【讨论】:
NHibernate 可以连接 MySQL 并且是免费的:
http://community.jboss.org/wiki/DatabasesSupportedByNHibernate
【讨论】:
Subsonic(开源)与 MySQL(5.0+)一起工作,特别支持 InnoDB -
【讨论】: