根据MySQL表创建C#类

是否有任何内置于.Net或visual studio中的内容可以让我根据MySql表创建类。 我想我在谈论持久性。 我只想让类成为表的1​​对1映射。 有什么免费的吗?

也许你需要这样的东西:

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

您可以使用entity framework。 它与MySQL连接良好。 我一直在关注本教程: http : //www.devart.com/dotconnect/mysql/articles/tutorial_ef.html

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

但是需要一些手工工作。

似乎有办法让EntityFramework与MySQL一起工作

将MySQL与Entity Framework结合使用

http://weblogs.asp.net/gunnarpeipman/archive/2010/12/09/getting-mysql-work-with-entity-framework-4-0.aspx

您还可以使用LINQ to SQL与MySQL。 但是,您应该研究一下,找到您必须安装的正确提供商。

我认为它几乎涵盖在这里:

LINQ to MySQL

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

http://community.jboss.org/wiki/DatabasesSupportedByNHibernate

Subsonic(开源)与MySQL(5.0+)一起使用,特别支持InnoDB –

http://subsonicproject.com/

我将NHibernate与MyGeneration一起使用

MyGeneration是一个程序,可以读取您的数据库模式并基于模板生成代码(在NHibernate的情况下,实体和映射)