<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://interactiveasp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Nate's Stuff : Tips &amp;amp; Tricks, Linq to SQL, Tools</title><link>http://interactiveasp.net/blogs/natesstuff/archive/tags/Tips+_2600_amp_3B00_+Tricks/Linq+to+SQL/Tools/default.aspx</link><description>Tags: Tips &amp;amp; Tricks, Linq to SQL, Tools</description><dc:language>en</dc:language><generator>CommunityServer 2008 (Build: 30417.1769)</generator><item><title>Easily Generate Business Objects From The Database</title><link>http://interactiveasp.net/blogs/natesstuff/archive/2008/09/23/easily-generate-business-objects-from-the-database.aspx</link><pubDate>Tue, 23 Sep 2008 17:48:21 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:88</guid><dc:creator>Nathan Zaugg</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/natesstuff/rsscomments.aspx?PostID=88</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://interactiveasp.net/blogs/natesstuff/commentapi.aspx?PostID=88</wfw:comment><comments>http://interactiveasp.net/blogs/natesstuff/archive/2008/09/23/easily-generate-business-objects-from-the-database.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 10px 5px 0px;border-left:0px;border-bottom:0px;" height="109" alt="netdev" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/netdev_5F00_3.gif" width="120" align="left" border="0" /&gt; I know the title of this post is a little bit of a misnomer, business objects will never map 1 to 1 with relational database objects.&amp;nbsp; However, when just starting out and when you get to control the schema of the database, it sure makes a good starting point and this trick makes it really easy and avoids a lot of typing.&amp;nbsp; It supports SQL 2005 and SQL 2008.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;The first step is to download the script. The script I wrote to do this can be found at &lt;a title="http://interactiveasp.net/media/p/87.aspx" href="http://interactiveasp.net/media/p/87.aspx"&gt;http://interactiveasp.net/media/p/87.aspx&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Once the script has been downloaded, open it in SQL Server Management Studio and fill in the fields for Database name and Table Name.&lt;/p&gt; &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/Set-Settings-for-Property-Generation_5F00_2.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="206" alt="Set Settings for Property Generation" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/Set-Settings-for-Property-Generation_5F00_thumb.png" width="454" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;After the settings are set run the script.&amp;nbsp; You should get something that looks a lot like:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/Result-Properties_5F00_4.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="317" alt="Result Properties" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/Result-Properties_5F00_thumb_5F00_1.png" width="454" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Next, select all of the rows that are part of the table you are creating the class for.&amp;nbsp; Notice that my selection excludes the first column.&amp;nbsp; After the rows are selected, copy the rows out of the results grid.&amp;nbsp; Next, open up your project in Visual Studio.&amp;nbsp; Create a new class with the appropriate name and paste the contents of the clipboard as shown below.&lt;/p&gt; &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/Paste-copied-Code_5F00_2.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="528" alt="Paste copied Code" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/Paste-copied-Code_5F00_thumb.png" width="454" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;The act of pasting the code into a class will cause the code to be formatted.&amp;nbsp; This should create nice looking properties inside of your class.&lt;/p&gt; &lt;p&gt;&lt;a href="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/Newly-Created-Class_5F00_2.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="569" alt="Newly Created Class" src="http://interactiveasp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/natesstuff/Newly-Created-Class_5F00_thumb.png" width="454" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;And that is all there is to it.&amp;nbsp; You will notice that by default the properties are sorted first by data type (desc), then by column name.&amp;nbsp; You can change this sort by changing the SQL Script if you wish.&amp;nbsp; I tend to like to group like-types together and then alphabetize.&amp;nbsp; I think it makes it a little more readable.&amp;nbsp; Also, we are using &lt;a href="http://msdn.microsoft.com/en-us/library/bb384054.aspx" target="_blank"&gt;auto-implemented properties&lt;/a&gt; (aka Automatic Properties).&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;If you use the script or have comments / suggestions, please leave a comment and let me know!&lt;/p&gt; &lt;h5&gt;Links&lt;/h5&gt; &lt;ul&gt; &lt;li&gt;&lt;a title="Property Generation SQL Script" href="http://interactiveasp.net/media/p/87.aspx"&gt;Property Generation SQL Script&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb384054.aspx" target="_blank"&gt;auto-implemented properties&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=88" width="1" height="1"&gt;</description><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/Tools/default.aspx">Tools</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/SQL+Server+2005/default.aspx">SQL Server 2005</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/Linq+to+SQL/default.aspx">Linq to SQL</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/Tips+_2600_amp_3B00_+Tricks/default.aspx">Tips &amp;amp; Tricks</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/SQL+Server+2008/default.aspx">SQL Server 2008</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/Open+Source/default.aspx">Open Source</category><category domain="http://interactiveasp.net/blogs/natesstuff/archive/tags/.NET/default.aspx">.NET</category></item></channel></rss>