views:

338

answers:

6

Using Visual Studio .NET 2008 or 2005, is there a way to automatically generate properties for each column in an SQL Server database table?

I am assuming other code-generation software exists that will do this. I know with Visio I can connect to my database and it will generate diagrams by table, this would be similar to that.

A: 

Check out MyGeneration

Anton Gogolev
+2  A: 

Linq2SQL in .NET 3.5 (VS2008) is what you're looking for.

Add a Linq2SQL DBML File to your solution/project

Create a connection to the Database with SErver Explorer

Drag and drop Tables/views/Sprocs

Code Generation is done for you... (and its damn cool :) )

Eoin Campbell
I haven't jumped into LINQ yet, I really wanted it to just generate the property code to save manually typing all of them out... but I got your suggestion up and running quickly so maybe it's time to jump into this.
mccrager
The beauty of this is that what gets generated are just collections of classes so even if you're not up to speed on LINQ/Lambda type functionality yet, you can still use these collections and iterate through them as you normally would.
Eoin Campbell
+1  A: 

mygeneration is really very cool and also lets you create your own templates giving you lot ore power to generate the "usual" classes for data access, entity classes, etc.

Vikram
+1  A: 

CodeSmith:

<%-- 
Name: Database Table Properties
Authors: Paul Welter , Yordan Georgiev
Description: Create a list of properties from a database table with a region for each prop
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

<% foreach (ColumnSchema column in this.SourceTable.Columns) {  %>

#region <%= StringUtil.ToPascalCase(column.Name) %>
private <%= CSharpAlias[column.SystemType.FullName] %> _<%= StringUtil.ToPascalCase(column.Name) %>;

public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToPascalCase(column.Name) %>
{
 get { return _<%= StringUtil.ToPascalCase(column.Name) %>; }
 set { _<%= StringUtil.ToPascalCase(column.Name) %> = value; }
}
#endregion <%= StringUtil.ToPascalCase(column.Name) %>
<% } %>
YordanGeorgiev
A: 

SubSonic / Rob Connery has blogged about using T4 as a tool for generating a Repository. T4 is in the VS 2005 SDK and native to VS 2008.

David Robbins
A: 

The Visual Studio DataSet designer can also accomplish this for you. It generates classes that wrap around your database objects quite easily. Just add a new DataSet to your application.

Navaar