views:

98

answers:

6

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.

A: 

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.

p.campbell
+1  A: 

How about the Linq to Sql O/R Designer?

spender
A: 

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

nrocha
Or even better with the copy/paste, just copy from SQL Server Management Studio in a table's design view.
p.campbell
A: 

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.

Winston Smith
A: 

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

Thanks for everyones input though

kurasa
A: 

You can also use MyGeneration

Matt