简体   繁体   English

从SQL Server表反向工程/创建C#接口

[英]reverse engineer/create c# interface from sql server table

does anyone know how of a tool where I can point to my sql server database and it reads the schema and generates c# interface classes from the tables? 有谁知道一种工具,可以指向我的sql server数据库,它可以读取架构并从表中生成c#接口类?

for example - say I have a table called 'Customers' with a "Name" column, "Address" Column and a "Phone" column it would generate a ICustomer.cs file with string Name {get;set;} string Address {get;set;} and int Phone {get;set;} 例如,假设我有一个名为“ Customers”的表,其中包含“名称”列,“地址”列和“电话”列,它将生成一个ICustomer.cs文件,该文件的字符串为Name {get; set;},字符串地址为{get ; set;}和int Phone {get; set;}

I am using a 'incomplete' code generator and it does not generate these interfaces. 我使用的是“不完整”代码生成器,它不会生成这些接口。

I don't know of a tool doing it, but I know for sure that you can do it yourself quite easily! 我不知道有什么工具可以执行此操作,但是我可以肯定,您可以自己轻松地执行此操作!

Set up a string containing the header of the class, and another with the footer. 设置一个包含类标题的字符串,另一个包含页脚的字符串。

Then, create a new text file named as your table. 然后,创建一个名为表的新文本文件。

Write the header into the file. 将标头写入文件。

For the body, just write a loop reading your table, which extracts the names and types of the fields, and writes an interface with that info. 对于主体,只需编写一个读取表的循环,即可提取字段的名称和类型,并使用该信息编写一个接口。

At the end, write the footer to the file. 最后,将页脚写入文件。

There you go with your brand new interface! 在那里,您可以使用全新的界面!

(As interfaces are juste plain text files, it's really easy...) (由于界面只是纯文本文件,因此非常容易...)

My SqlSharpener project lets you parse SQL files at design-time to create a meta-model which you can then use to generate any type of code you like in a T4 template. 我的SqlSharpener项目使您可以在设计时解析SQL文件以创建一个元模型,然后可以使用该元模型在T4模板中生成您喜欢的任何类型的代码。 For example, you could create Entity Framework Code First entities. 例如,您可以创建“ 实体框架代码优先”实体。

You could use SQLMetal.exe to get part of the job done. 您可以使用SQLMetal.exe来完成部分工作。 IT sounds like you want an interface, but this will create concrete classes. IT听起来像您想要一个接口,但这将创建具体的类。 It'd be a small task to find/replace class with interface , and modify the names. interface查找/替换class并修改名称将是一个小任务。

  • C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin\\SqlMetal.exe or open a Visual Studio Command Prompt. C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin\\SqlMetal.exe或打开Visual Studio命令提示符。
  • Usage: sqlmetal /server:myserver /database:myDB /user:myUser /pwd:myPwd /language:csharp /code:myDB.cs 用法: sqlmetal /server:myserver /database:myDB /user:myUser /pwd:myPwd /language:csharp /code:myDB.cs

More options over at the MSDN page for SQLMetal. 有关SQLMetal的更多信息,请访问MSDN页面。

You need a tool that allows you to customize your code generation templates. 您需要一个可以自定义代码生成模板的工具。 Have you considered Enterprise Architect , or CodeSmith ? 您是否考虑过Enterprise ArchitectCodeSmith

There are numerous others - you may even want to go for a Model Driven Architecture. 还有很多其他的-您甚至可能想要使用模型驱动的体系结构。 Design your solution in UML and generate the database and the code from the UML model. 使用UML设计解决方案,并从UML模型生成数据库和代码。 You can use a combination of tools for this, for example MagicDraw and Maven. 您可以为此使用多种工具的组合,例如MagicDraw和Maven。

I think I can use Resharpers 'Extract Interface' refactor as I have generated the class already. 我想我可以使用Resharpers的“提取接口”重构,因为我已经生成了该类。

Thanks for everyones input though 谢谢大家的投入

您也可以使用MyGeneration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM