<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.dotnetmafia.com/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>Corey Roth [MVP] : ASP.NET, LINQ to SQL, LINQ</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/ASP.NET/LINQ+to+SQL/LINQ/default.aspx</link><description>Tags: ASP.NET, LINQ to SQL, LINQ</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>How to: Use LINQ to SQL without using the Object Relational Designer</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/17/how-to-use-linq-to-sql-without-using-the-object-relational-designer.aspx</link><pubDate>Tue, 18 Mar 2008 00:54:20 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:537</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=537</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/17/how-to-use-linq-to-sql-without-using-the-object-relational-designer.aspx#comments</comments><description>&lt;p&gt;LINQ to SQL has already proved to be extremely easy to use to create object relational mappings when you have an existing database schema using the Object Relational Designer.&amp;nbsp; This designer is good, but you may not want something that is autogenerating your domain classes.&amp;nbsp; You may want to generate your domain classes yourself.&amp;nbsp; This is actually quite easy and works in a similar manner to other OR/Ms such as ActiveRecord.&amp;nbsp; The thing I like about it is that your domain objects do not have to inherit from some base class that has all of the underlying logic to access the database.&amp;nbsp; Instead you create a custom class separate from your domain objects that inherits from DataContext.&lt;/p&gt; &lt;p&gt;We&amp;#39;ll create a simple example of a products table for an e-Commerce web site.&amp;nbsp; Let&amp;#39;s start by looking at the domain object.&amp;nbsp; Before you create your domain object start by adding a reference to &lt;em&gt;System.Data.Linq&lt;/em&gt; to your class library if it is not already present.&amp;nbsp; You will then need to add a using statement in each domain class for &lt;em&gt;System.Data.Linq.Mapping&lt;/em&gt;.&lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;[&lt;span style="color:#2b91af;"&gt;Table&lt;/span&gt;(Name=&lt;span style="color:#a31515;"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;)]&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Product&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;{&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color:#2b91af;"&gt;Column&lt;/span&gt;]&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt; Name;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color:#2b91af;"&gt;Column&lt;/span&gt;(IsPrimaryKey=&lt;span style="color:blue;"&gt;true&lt;/span&gt;, Name=&lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)]&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; ProductId;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color:#2b91af;"&gt;Column&lt;/span&gt;(Name=&lt;span style="color:#a31515;"&gt;&amp;quot;Price&amp;quot;&lt;/span&gt;)]&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;double&lt;/span&gt; Price;&lt;/p&gt; &lt;p style="margin:0px;"&gt;}&lt;/p&gt;&lt;/div&gt; &lt;p&gt;The first thing you do in your domain class is decorate it with a &lt;strong&gt;Table&lt;/strong&gt; attribute.&amp;nbsp; An optional parameter here specifies the name of the underlying database table.&amp;nbsp; In this case my domain object it Product but my database table is named Products.&amp;nbsp; I then defined three properties representing columns in the table.&amp;nbsp; The &lt;strong&gt;Column&lt;/strong&gt; attribute specifies that the property will have a corresponding column in a database table.&amp;nbsp; The &lt;em&gt;IsPrimaryKey&lt;/em&gt; parameter specifies that the column is a primary key in the database.&amp;nbsp; The &lt;em&gt;Name&lt;/em&gt; parameter here also allows you to specify a different column name in the database.&lt;/p&gt; &lt;p&gt;That is really all that is required to create a domain object.&amp;nbsp; You can define a class for each domain object you want and you can also create relations between them (but I won&amp;#39;t be covering that here today).&amp;nbsp; Once you have your domain object created, you will need to create a DataContext class to actually be able to query your domain objects.&amp;nbsp; This is also pretty simple.&amp;nbsp; You just expose a property with the generic type of Table&amp;lt;&amp;gt; for each one of your domain objects.&amp;nbsp; The name of the property is what you will use with the DataContext when you are querying with LINQ.&lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;StoreDataContext&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;DataContext&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;{&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Table&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Product&lt;/span&gt;&amp;gt; Products;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; StoreDataContext(&lt;span style="color:blue;"&gt;string&lt;/span&gt; connection)&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;span style="color:blue;"&gt;base&lt;/span&gt;(connection)&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin:0px;"&gt;}&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Now that you have your domain objects written you will need to create the SQL tables that they represent.&amp;nbsp; You can do this manually, or you can have LINQ create the whole database for you.&amp;nbsp; Just create an instance of your DataContext and call the &lt;em&gt;CreateDatabase&lt;/em&gt; method.&amp;nbsp; This method infers the name of the database given the connection string you used.&amp;nbsp; If you did not specify the database, you need to add a &lt;strong&gt;Database&lt;/strong&gt; attribute with the name to your class.&lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;StoreDataContext&lt;/span&gt; myDataContext = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;StoreDataContext&lt;/span&gt;(myConnectionString);&lt;/p&gt; &lt;p style="margin:0px;"&gt;myDataContext.CreateDatabase();&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Alright, so now your domain objects and database are created, now you just need to query something with it.&lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; products = &lt;span style="color:blue;"&gt;from&lt;/span&gt; product &lt;span style="color:blue;"&gt;in&lt;/span&gt; myDataContext.Products&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;where&lt;/span&gt; product.Price &amp;gt; 49.99f&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;select&lt;/span&gt; product;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;This simple query simply returns any product with a price greater than 49.99.&amp;nbsp; So, LINQ to SQL doesn&amp;#39;t have to be completely domain driven.&amp;nbsp; This gives you a lot of flexibility and makes it easy to add additional things to your domain logic if you want to.&amp;nbsp; The downside to this of course it that, when your database schema changes, your domain object is not going to get updated at the click of a button.&amp;nbsp; If you are building your domain objects in this manner though, this is probably not a concern to you though.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=537" width="1" height="1"&gt;</description><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Visual+Studio+2008/default.aspx">Visual Studio 2008</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/C_2300_+3.0/default.aspx">C# 3.0</category></item></channel></rss>