在Visual Studio中为表生成C#类代码

m3n*_*haq 4 c# class visual-studio-2010 auto-generate

我的Server Explore数据库文件中有多个表.我想为表生成类的自动代码,因此所有具有属性,构造函数和getter setter方法的类都会自动生成.

请告诉我这样做的步骤.

jmo*_*eno 5

不自动生成,但是使用sql和information_schema输出类定义并不难,使用以表命名的类和要映射到属性的列.从那里你可以让它生成创建,更新和删除(我喜欢使用合并为SQL Server 2008下的包/更新).

一次做一个,主要是字符串连接.以下应该让你开始......

declare @class varchar(max);

; with typemapping as (
 Select 'varchar' as DATA_TYPE, 'string' ctype
 union
 Select 'int', 'int'
)
select @class = isnull(@class + char(10), '') + 'public ' +
       tm.ctype +' ' + column_name +
       ' { get; set; }'
from information_schema.columns sc
   inner join typemapping tm on sc.data_type = tm.data_type
where table _name ='yourtbl'

print @class;
Run Code Online (Sandbox Code Playgroud)

其余部分留给读者,因为他们说主要是因为细节取决于你,而不是你可以使用支持变量的自动属性,在属性中放置标准逻辑,使值类型可以为空,在制作自己的代码时发电机,使其适合您的模式/风格/需求.