reverse engineer/create c# interface from sql server table

2.1k views Asked by At

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.

7

There are 7 answers

0
kurasa On BEST ANSWER

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

Thanks for everyones input though

0
p.campbell On

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.

0
spender On

How about the Linq to Sql O/R Designer?

1
nrocha On

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

0
Winston Smith On

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.

0
adam0101 On

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.

0
Matt H On

You can also use MyGeneration