逆向工程/从 sql server 表创建 C# 接口

发布于 2024-09-24 14:50:58 字数 240 浏览 0 评论 0原文

有谁知道如何使用一个工具来指向我的 sql server 数据库并读取架构并从表中生成 C# 接口类?

例如 - 假设我有一个名为“客户”的表,其中包含“名称”列、“地址”列和“电话”列,它将生成一个 ICustomer.cs 文件,其中包含字符串 Name {get;set;} string Address {get ;set;} 和 int Phone {get;set;}

我使用的是“不完整”代码生成器,它不会生成这些接口。

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?

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

I am using a 'incomplete' code generator and it does not generate these interfaces.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

魂归处 2024-10-01 14:50:58

我不知道有什么工具可以做到这一点,但我确信您可以自己轻松完成!

设置一个包含类标题的字符串,另一个包含页脚的字符串。

然后,创建一个名为您的表的新文本文件。

将标头写入文件。

对于主体,只需编写一个读取表的循环,该循环提取字段的名称和类型,并使用该信息编写一个接口。

最后,将页脚写入文件。

全新的界面就到此为止了!

(由于界面只是纯文本文件,因此非常简单......)

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

肩上的翅膀 2024-10-01 14:50:58

您可以使用 SQLMetal.exe 来完成部分工作。听起来您想要一个接口,但这将创建具体的类。用interface查找/替换class并修改名称是一个小任务。

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

更多选项请参见 SQLMetal 的 MSDN 页面。

You could use SQLMetal.exe to get part of the job done. IT sounds like you want an interface, but this will create concrete classes. It'd be a small task to find/replace class with interface, and modify the names.

  • C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\SqlMetal.exe or open a Visual Studio Command Prompt.
  • Usage: sqlmetal /server:myserver /database:myDB /user:myUser /pwd:myPwd /language:csharp /code:myDB.cs

More options over at the MSDN page for SQLMetal.

2024-10-01 14:50:58

您需要一个允许您自定义代码生成模板的工具。您是否考虑过企业架构师,或CodeSmith

还有许多其他的 - 您甚至可能想要采用模型驱动架构。使用 UML 设计您的解决方案并从 UML 模型生成数据库和代码。为此,您可以使用多种工具的组合,例如 MagicDraw 和 Maven。

You need a tool that allows you to customize your code generation templates. Have you considered Enterprise Architect, or CodeSmith?

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. You can use a combination of tools for this, for example MagicDraw and Maven.

三人与歌 2024-10-01 14:50:58

我想我可以使用 Resharpers 'Extract Interface' 重构,因为我已经生成了该类。

不过还是感谢大家的意见

I think I can use Resharpers 'Extract Interface' refactor as I have generated the class already.

Thanks for everyones input though

十年不长 2024-10-01 14:50:58

我的 SqlSharpener 项目可让您在设计时<解析 SQL 文件/strong> 创建一个元模型,然后您可以使用它在 T4 模板中生成您喜欢的任何类型的代码。例如,您可以创建实体框架代码优先< /a> 实体。

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. For example, you could create Entity Framework Code First entities.

愿与i 2024-10-01 14:50:58

您还可以使用 MyGeneration

You can also use MyGeneration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文