简体   繁体   中英

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?

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.

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

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.

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.

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

Thanks for everyones input though

您也可以使用MyGeneration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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