C# 基于MySQL表创建C类

C# 基于MySQL表创建C类,c#,.net,mysql,database,persistence,C#,.net,Mysql,Database,Persistence,Net或visual studio中是否内置了任何东西,允许我基于MySql表创建类。我想我说的是坚持。我只希望该类是表的1对1映射。有免费的吗?似乎有一种方法可以让EntityFramework使用MySQL 您还可以将LINQtoSQL与MySQL结合使用。但是,您应该进行一些研究,以找到需要安装的正确提供商 我想这里基本上涵盖了这一点: NHibernate可以连接到MySQL,并且是免费的: 亚音速开源与MySQL 5.0+配合使用,特别支持InnoDB- 我用NHibernate和 M

Net或visual studio中是否内置了任何东西,允许我基于MySql表创建类。我想我说的是坚持。我只希望该类是表的1对1映射。有免费的吗?

似乎有一种方法可以让EntityFramework使用MySQL


您还可以将LINQtoSQL与MySQL结合使用。但是,您应该进行一些研究,以找到需要安装的正确提供商

我想这里基本上涵盖了这一点:


NHibernate可以连接到MySQL,并且是免费的:


亚音速开源与MySQL 5.0+配合使用,特别支持InnoDB-

我用NHibernate和


MyGeneration是一个程序,它可以读取您的数据库模式,并根据模板生成代码。对于NHibernate,实体和映射可以使用实体框架进行此操作。它与MySQL连接良好。
我一直在学习本教程:

也许你需要这样的东西:

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 '}';

我调整了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

希望能有帮助。干杯

以下是所做的伟大工作:

创建一个过程,如下所示:

-- --------------------------------------------------------------------------------
-- 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

但是,需要一些手动操作。

第一个示例非常好,但是缺少一些类型,因此我正在共享添加缺少的类型集、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 '}';

@KrisKrause是的,事实上我确实使用fluent,但我认为OP对MyGeneration更感兴趣:[……]这将允许我基于MySql tableNice创建类;在紧要关头需要一个快速的方法来做这件事。创建了一个带有一些小改动的要点。非常出色的轻量级解决方案。第一次工作。使用select concat'public',tps.dest,IFtps.dest='string',IFis_nullable='NO','?'。。。然后移除?从正确的可空映射的映射中,如果您的数据库具有tinytext数据类型,您可以添加select“tinytext”,“string”union all使用ifc.IS_NULLable=“NO”,REPLACEtps.dest,“?”,而不是tps.dest该链接指向一个被入侵的网站