<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Michael Maddox - Software Development - AFO</title>
    <link>http://www.capprime.com/software_development_weblog/</link>
    <description>Best Practices, Knowlegde Base Type Articles, General Tech Opinions, etc.</description>
    <language>en-us</language>
    <copyright>Michael Maddox</copyright>
    <lastBuildDate>Sun, 01 Aug 2010 11:24:48 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>Michael.P.Maddox@gmail.com</managingEditor>
    <webMaster>Michael.P.Maddox@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.capprime.com/software_development_weblog/Trackback.aspx?guid=d404eb9a-57bf-46e0-976a-9f81312ef5a8</trackback:ping>
      <pingback:server>http://www.capprime.com/software_development_weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.capprime.com/software_development_weblog/PermaLink,guid,d404eb9a-57bf-46e0-976a-9f81312ef5a8.aspx</pingback:target>
      <dc:creator>Michael Maddox</dc:creator>
      <wfw:comment>http://www.capprime.com/software_development_weblog/CommentView,guid,d404eb9a-57bf-46e0-976a-9f81312ef5a8.aspx</wfw:comment>
      <wfw:commentRss>http://www.capprime.com/software_development_weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=d404eb9a-57bf-46e0-976a-9f81312ef5a8</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This post is an update to a post I made last September (~10 months ago):
</p>
        <p>
          <a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,162facaa-a3b9-4dc5-b770-657e27e887ad.aspx">http://www.capprime.com/software_development_weblog/PermaLink,guid,162facaa-a3b9-4dc5-b770-657e27e887ad.aspx</a>
        </p>
        <p>
The biggest roadblock I ran into when writing that blog post is that ActiveWriter
doesn't work well with the Northwind database which had significant ripple effects
throughout the sample.
</p>
        <p>
That inspired me to spend quite a bit of time over the last 10 months to make the
Castle ActiveRecord code generation story better.
</p>
        <p>
So here is an updated sample using the free version of the <a href="http://www.agilityfororms.com/Home/Products/AfoCastleActiveRecordModelCodeGenerator/">Agility
for ORMs Castle ActiveRecord code generator</a> in place of ActiveWriter.
</p>
        <p>
--
</p>
        <p>
This is a quick guide to getting up and running with NHibernate and Linq quickly.
</p>
        <p>
We are going to assume our database already exists.  We are going to assume that
database is Northwind, and we are going to assume that we are doing database driven
design (as opposed to domain driven design).  Northwind setup is described below.
</p>
        <p>
We are going to use Visual Studio 2008 with Service Pack 1 and SQL Server 2008 Express. 
(Note: A web application variant of this should work with Visual Web Developer 2008
Express Edition resulting in a completely free development stack.  This sample
should also work fine in Visual Studio 2010, but you'll need to change the project
to target the .NET Full Profile instead of the .NET Client Profile.)
</p>
        <p>
Step 1:
</p>
        <p>
Download and install the Northwind database.
</p>
        <p>
Jeff Atwood provides approximate instructions here (ask specific questions in the
comments if you get stuck):
</p>
        <p>
          <a href="http://www.codinghorror.com/blog/archives/000434.html">http://www.codinghorror.com/blog/archives/000434.html</a>
        </p>
        <p>
Follow the path for installing the binary files from the command line.  I have
tested that the SQL 2005 instructions work on SQL 2008.
</p>
        <p>
          <br />
Step 2:
</p>
        <p>
Next Download Castle ActiveRecord.  The download link is available from here:
</p>
        <p>
          <a href="http://www.castleproject.org/castle/projects.html">http://www.castleproject.org/castle/projects.html</a>
        </p>
        <p>
At the time of writing, the current version is "2.1.2 (2010-01-31)".
</p>
        <p>
Unzip it and remember where you put it, you'll need that info in step 3.
</p>
        <p>
          <br />
Step 3:
</p>
        <p>
Start Visual Studio 2008
</p>
        <p>
We will create a new console application project:
</p>
        <p>
File -&gt; New -&gt; Project<br />
 Visual C# -&gt; Windows -&gt; Console Application<br />
  ConsoleApplication1 -&gt; OK
</p>
        <p>
We need to add the appropriate NHibernate &amp; Castle Active Record references:
</p>
        <p>
Solution Explorer<br />
 Right Click ConsoleApplication1 -&gt; References -&gt; Add<br />
  Browse Tab<br />
   Go to your Castle ActiveRecord download location and add:<br />
    Castle.ActiveRecord.dll<br />
    Castle.ActiveRecord.Linq.dll<br />
    Castle.DynamicProxy2.dll<br />
    NHibernate.dll<br />
    NHibernate.Linq.dll<br />
   Click OK
</p>
        <p>
And add a reference to System.Configuration as well:
</p>
        <p>
Solution Explorer<br />
 Right Click ConsoleApplication1 -&gt; References -&gt; Add<br />
  .NET Tab<br />
   System.Configuration<br />
  Click OK
</p>
        <p>
          <br />
Step 4:
</p>
        <p>
Download the free (aka Convention Only) version of the Agility for ORMs Castle ActiveRecord
code generator by registering and then downloading here:
</p>
        <p>
          <a href="http://www.agilityfororms.com/Apps/Register.aspx">http://www.agilityfororms.com/Apps/Register.aspx</a>
        </p>
        <p>
The current version is 1.0.0.4.
</p>
        <p>
          <br />
Step 5:
</p>
        <p>
Run the AFO Castle ActiveRecord code generator, specifying the correct connection
string for the Northwind database you setup in Step 1 above.
</p>
        <p>
Note the output directory where the generated files went, you'll need that info in
step 6.
</p>
        <p>
          <br />
Step 6:
</p>
        <p>
Add the generated files to the console application project.
</p>
        <p>
Solution Explorer<br />
 Right Click ConsoleApplication1 -&gt; Add -&gt; New Folder -&gt; DataLayer<br />
 Right Click DataLayer -&gt; Add -&gt; Existing Item...<br />
  Add all of the .cs files generated in Step 5.
</p>
        <p>
          <br />
Step 7:
</p>
        <p>
We will now add an Application Configuration File to the project and put the Northwind
Connection String into it:
</p>
        <p>
Solution Explorer<br />
 Right Click ConsoleApplication1 -&gt; Add -&gt; New Item...<br />
  Visual C# Items -&gt; General -&gt; Application Configuration File<br />
  Add
</p>
        <p>
Modify the file to look like this and use your specific DB connection info:
</p>
        <p>
--<br />
&lt;?xml version="1.0" encoding="utf-8" ?&gt;<br />
&lt;configuration&gt;<br />
  &lt;connectionStrings&gt;<br />
    &lt;add name="Northwind" connectionString="Data Source=.\SQLExpress;Initial
Catalog=Northwind;Trusted_Connection=True;"/&gt;<br />
  &lt;/connectionStrings&gt;<br />
&lt;/configuration&gt;<br />
--
</p>
        <p>
          <br />
Step 8:
</p>
        <p>
Open the main "Program.cs" class and add the following new method:
</p>
        <p>
        private static void InitializeNHibernateActiveRecord()<br />
        {<br />
            string connectionString
= System.Configuration.ConfigurationManager.ConnectionStrings["Northwind"].ToString();<br />
            InPlaceConfigurationSource
configuration = InPlaceConfigurationSource.Build(DatabaseType.MsSqlServer2008, connectionString);
</p>
        <p>
            ActiveRecordStarter.Initialize(System.Reflection.Assembly.GetExecutingAssembly(),
configuration);<br />
        }
</p>
        <p>
Add the following code to the Main Method:
</p>
        <p>
        static void Main(string[] args)<br />
        {<br />
            try<br />
            {<br />
               
InitializeNHibernateActiveRecord();
</p>
        <p>
               
using (new SessionScope())<br />
               
{<br />
                   
var queryToExecute = from oneProduct in ActiveRecordLinq.AsQueryable&lt;Product&gt;()<br />
                                        
select oneProduct;
</p>
        <p>
                   
foreach (Product oneProduct in queryToExecute.Take(5).ToList())<br />
                   
{<br />
                       
Console.WriteLine("ProductID=[" + oneProduct.ProductID + "] ProductName=[" + oneProduct.ProductName
+ "] Supplier CompanyName=[" + oneProduct.Supplier.CompanyName + "]");<br />
                   
}<br />
               
}<br />
            }<br />
            catch (Exception
oneException)<br />
            {<br />
               
Console.WriteLine("oneException=[" + oneException + "]");<br />
               
throw; // you can remove this if you'd rather the program exit "more normally"<br />
            }<br />
        }
</p>
        <p>
Add the following using statements at the top of Program.cs:
</p>
        <p>
using Castle.ActiveRecord;<br />
using Castle.ActiveRecord.Framework.Config;<br />
using Castle.ActiveRecord.Linq;
</p>
        <p>
using Model.Northwind;
</p>
        <p>
          <br />
Step 9:
</p>
        <p>
Copy the NHibernate.ByteCode.Castle.dll file from your Castle Active Record download
unzip directory to your projects bin\Debug\ folder.
</p>
        <p>
          <br />
Step 10:
</p>
        <p>
Run the application:
</p>
        <p>
Ctrl-F5 (Debug -&gt; Start Without Debugging)
</p>
        <p>
And you should see the following output:
</p>
        <p>
--<br />
ProductID=[1] ProductName=[Chai] Supplier CompanyName=[Exotic Liquids]<br />
ProductID=[2] ProductName=[Chang] Supplier CompanyName=[Exotic Liquids]<br />
ProductID=[3] ProductName=[Aniseed Syrup] Supplier CompanyName=[Exotic Liquids]<br />
ProductID=[4] ProductName=[Chef Anton's Cajun Seasoning] Supplier CompanyName=[New
Orleans Cajun Delights]<br />
ProductID=[5] ProductName=[Chef Anton's Gumbo Mix] Supplier CompanyName=[New Orleans
Cajun Delights]<br />
Press any key to continue . . .<br />
--
</p>
        <p>
We have successfully executed a join query through Linq.
</p>
        <p>
          <br />
Please note:
</p>
        <p>
You'll notice that the Agility for ORMs Castle ActiveRecord code generator only generated
5 class files in addition to a readme.txt file (there are 13 tables in Northwind which
would otherwise result in 11 class files).  Northwind is not a convention based
database due to use of assignable keys, surrogate keys, and composite keys. 
The free version of the AFO code generator only works with convention oriented database
tables and only 5 of the tables in Northwind follow conventions.  The readme.txt
file explains why the other tables were not generated.
</p>
        <p>
The commercial version ($30) of the AFO code generator will properly generate code
for all 13 tables in Northwind and is intended to support the entire feature set of
Castle ActiveRecord including things like Composite Keys, which aren't currently supported
by ActiveWriter.
</p>
        <p>
          <br />
Now that you have the basic NHibernate Linq infrastructure in place, there are plenty
of Linq examples and sample code available elsewhere.
</p>
        <p>
Enjoy!
</p>
        <img width="0" height="0" src="http://www.capprime.com/software_development_weblog/aggbug.ashx?id=d404eb9a-57bf-46e0-976a-9f81312ef5a8" />
      </body>
      <title>Using Linq with NHibernate : A Quick Start - Updated August 2010</title>
      <guid isPermaLink="false">http://www.capprime.com/software_development_weblog/PermaLink,guid,d404eb9a-57bf-46e0-976a-9f81312ef5a8.aspx</guid>
      <link>http://www.capprime.com/software_development_weblog/2010/08/01/UsingLinqWithNHibernateAQuickStartUpdatedAugust2010.aspx</link>
      <pubDate>Sun, 01 Aug 2010 11:24:48 GMT</pubDate>
      <description>&lt;p&gt;
This post is an update to a post I made last September (~10 months ago):
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.capprime.com/software_development_weblog/PermaLink,guid,162facaa-a3b9-4dc5-b770-657e27e887ad.aspx"&gt;http://www.capprime.com/software_development_weblog/PermaLink,guid,162facaa-a3b9-4dc5-b770-657e27e887ad.aspx&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The biggest roadblock I ran into when writing that blog post is that ActiveWriter
doesn't work well with the Northwind database which had significant ripple effects
throughout the sample.
&lt;/p&gt;
&lt;p&gt;
That inspired me to spend quite a bit of time over the last 10 months to make the
Castle ActiveRecord code generation story better.
&lt;/p&gt;
&lt;p&gt;
So here is an updated sample using the free version of the &lt;a href="http://www.agilityfororms.com/Home/Products/AfoCastleActiveRecordModelCodeGenerator/"&gt;Agility
for ORMs Castle ActiveRecord code generator&lt;/a&gt;&amp;nbsp;in place of ActiveWriter.
&lt;/p&gt;
&lt;p&gt;
--
&lt;/p&gt;
&lt;p&gt;
This is a quick guide to getting up and running with NHibernate and Linq quickly.
&lt;/p&gt;
&lt;p&gt;
We are going to assume our database already exists.&amp;nbsp; We are going to assume that
database is Northwind, and we are going to assume that we are doing database driven
design (as opposed to domain driven design).&amp;nbsp; Northwind setup is described below.
&lt;/p&gt;
&lt;p&gt;
We are going to use Visual Studio 2008 with Service Pack 1 and SQL Server 2008 Express.&amp;nbsp;
(Note: A web application variant of this should work with Visual Web Developer 2008
Express Edition resulting in a completely free development stack.&amp;nbsp; This sample
should also work fine in Visual Studio 2010, but you'll need to change the project
to target the .NET Full Profile instead of the .NET Client Profile.)
&lt;/p&gt;
&lt;p&gt;
Step 1:
&lt;/p&gt;
&lt;p&gt;
Download and install the Northwind database.
&lt;/p&gt;
&lt;p&gt;
Jeff Atwood provides approximate instructions here (ask specific questions in the
comments if you get stuck):
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.codinghorror.com/blog/archives/000434.html"&gt;http://www.codinghorror.com/blog/archives/000434.html&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Follow the path for installing the binary files from the command line.&amp;nbsp; I have
tested that the SQL 2005 instructions work on SQL 2008.
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Step 2:
&lt;/p&gt;
&lt;p&gt;
Next Download Castle ActiveRecord.&amp;nbsp; The download link is available from here:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.castleproject.org/castle/projects.html"&gt;http://www.castleproject.org/castle/projects.html&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
At the time of writing, the current version is "2.1.2 (2010-01-31)".
&lt;/p&gt;
&lt;p&gt;
Unzip it and remember where you put it, you'll need that info in step 3.
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Step 3:
&lt;/p&gt;
&lt;p&gt;
Start Visual Studio 2008
&lt;/p&gt;
&lt;p&gt;
We will create a new console application project:
&lt;/p&gt;
&lt;p&gt;
File -&amp;gt; New -&amp;gt; Project&lt;br&gt;
&amp;nbsp;Visual C# -&amp;gt; Windows -&amp;gt; Console Application&lt;br&gt;
&amp;nbsp;&amp;nbsp;ConsoleApplication1 -&amp;gt; OK
&lt;/p&gt;
&lt;p&gt;
We need to add the appropriate NHibernate &amp;amp; Castle Active Record references:
&lt;/p&gt;
&lt;p&gt;
Solution Explorer&lt;br&gt;
&amp;nbsp;Right Click ConsoleApplication1 -&amp;gt; References -&amp;gt; Add&lt;br&gt;
&amp;nbsp;&amp;nbsp;Browse Tab&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Go to your Castle ActiveRecord download location and add:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Castle.ActiveRecord.dll&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Castle.ActiveRecord.Linq.dll&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Castle.DynamicProxy2.dll&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NHibernate.dll&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NHibernate.Linq.dll&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Click OK
&lt;/p&gt;
&lt;p&gt;
And add a reference to System.Configuration as well:
&lt;/p&gt;
&lt;p&gt;
Solution Explorer&lt;br&gt;
&amp;nbsp;Right Click ConsoleApplication1 -&amp;gt; References -&amp;gt; Add&lt;br&gt;
&amp;nbsp;&amp;nbsp;.NET Tab&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Configuration&lt;br&gt;
&amp;nbsp;&amp;nbsp;Click OK
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Step 4:
&lt;/p&gt;
&lt;p&gt;
Download the free (aka Convention Only) version of the Agility for ORMs Castle ActiveRecord
code generator by registering and then downloading here:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.agilityfororms.com/Apps/Register.aspx"&gt;http://www.agilityfororms.com/Apps/Register.aspx&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The current version is 1.0.0.4.
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Step 5:
&lt;/p&gt;
&lt;p&gt;
Run the AFO Castle ActiveRecord code generator, specifying the correct connection
string for the Northwind database you setup in Step&amp;nbsp;1 above.
&lt;/p&gt;
&lt;p&gt;
Note the output directory where the generated files went, you'll need that info in
step 6.
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Step 6:
&lt;/p&gt;
&lt;p&gt;
Add the generated files to the console application project.
&lt;/p&gt;
&lt;p&gt;
Solution Explorer&lt;br&gt;
&amp;nbsp;Right Click ConsoleApplication1 -&amp;gt; Add -&amp;gt; New Folder -&amp;gt; DataLayer&lt;br&gt;
&amp;nbsp;Right Click DataLayer -&amp;gt; Add -&amp;gt; Existing Item...&lt;br&gt;
&amp;nbsp;&amp;nbsp;Add all of the .cs files generated in Step 5.
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Step 7:
&lt;/p&gt;
&lt;p&gt;
We will now add an Application Configuration File to the project and put the Northwind
Connection String into it:
&lt;/p&gt;
&lt;p&gt;
Solution Explorer&lt;br&gt;
&amp;nbsp;Right Click ConsoleApplication1 -&amp;gt; Add -&amp;gt; New Item...&lt;br&gt;
&amp;nbsp;&amp;nbsp;Visual C# Items -&amp;gt; General -&amp;gt; Application Configuration File&lt;br&gt;
&amp;nbsp;&amp;nbsp;Add
&lt;/p&gt;
&lt;p&gt;
Modify the file to look like this and use your specific DB connection info:
&lt;/p&gt;
&lt;p&gt;
--&lt;br&gt;
&amp;lt;?xml version="1.0" encoding="utf-8" ?&amp;gt;&lt;br&gt;
&amp;lt;configuration&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;connectionStrings&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;add name="Northwind" connectionString="Data Source=.\SQLExpress;Initial
Catalog=Northwind;Trusted_Connection=True;"/&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;/connectionStrings&amp;gt;&lt;br&gt;
&amp;lt;/configuration&amp;gt;&lt;br&gt;
--
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Step 8:
&lt;/p&gt;
&lt;p&gt;
Open the main "Program.cs" class and add the following new method:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private static void InitializeNHibernateActiveRecord()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string connectionString
= System.Configuration.ConfigurationManager.ConnectionStrings["Northwind"].ToString();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; InPlaceConfigurationSource
configuration = InPlaceConfigurationSource.Build(DatabaseType.MsSqlServer2008, connectionString);
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ActiveRecordStarter.Initialize(System.Reflection.Assembly.GetExecutingAssembly(),
configuration);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p&gt;
Add the following code to the Main Method:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; static void Main(string[] args)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&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;&amp;nbsp;
InitializeNHibernateActiveRecord();
&lt;/p&gt;
&lt;p&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;&amp;nbsp;
using (new SessionScope())&lt;br&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;&amp;nbsp;
{&lt;br&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
var queryToExecute = from oneProduct in ActiveRecordLinq.AsQueryable&amp;lt;Product&amp;gt;()&lt;br&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;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
select oneProduct;
&lt;/p&gt;
&lt;p&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
foreach (Product oneProduct in queryToExecute.Take(5).ToList())&lt;br&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
{&lt;br&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Console.WriteLine("ProductID=[" + oneProduct.ProductID + "] ProductName=[" + oneProduct.ProductName
+ "] Supplier CompanyName=[" + oneProduct.Supplier.CompanyName + "]");&lt;br&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;br&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;&amp;nbsp;
}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception
oneException)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&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;&amp;nbsp;
Console.WriteLine("oneException=[" + oneException + "]");&lt;br&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;&amp;nbsp;
throw; // you can remove this if you'd rather the program exit "more normally"&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p&gt;
Add the following using statements at the top of Program.cs:
&lt;/p&gt;
&lt;p&gt;
using Castle.ActiveRecord;&lt;br&gt;
using Castle.ActiveRecord.Framework.Config;&lt;br&gt;
using Castle.ActiveRecord.Linq;
&lt;/p&gt;
&lt;p&gt;
using Model.Northwind;
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Step 9:
&lt;/p&gt;
&lt;p&gt;
Copy the NHibernate.ByteCode.Castle.dll file from your Castle Active Record download
unzip directory to your projects bin\Debug\ folder.
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Step 10:
&lt;/p&gt;
&lt;p&gt;
Run the application:
&lt;/p&gt;
&lt;p&gt;
Ctrl-F5 (Debug -&amp;gt; Start Without Debugging)
&lt;/p&gt;
&lt;p&gt;
And you should see the following output:
&lt;/p&gt;
&lt;p&gt;
--&lt;br&gt;
ProductID=[1] ProductName=[Chai] Supplier CompanyName=[Exotic Liquids]&lt;br&gt;
ProductID=[2] ProductName=[Chang] Supplier CompanyName=[Exotic Liquids]&lt;br&gt;
ProductID=[3] ProductName=[Aniseed Syrup] Supplier CompanyName=[Exotic Liquids]&lt;br&gt;
ProductID=[4] ProductName=[Chef Anton's Cajun Seasoning] Supplier CompanyName=[New
Orleans Cajun Delights]&lt;br&gt;
ProductID=[5] ProductName=[Chef Anton's Gumbo Mix] Supplier CompanyName=[New Orleans
Cajun Delights]&lt;br&gt;
Press any key to continue . . .&lt;br&gt;
--
&lt;/p&gt;
&lt;p&gt;
We have successfully executed a join query through Linq.
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Please note:
&lt;/p&gt;
&lt;p&gt;
You'll notice that the Agility for ORMs Castle ActiveRecord code generator only generated
5 class files in addition to a readme.txt file (there are 13 tables in Northwind which
would otherwise result in 11 class files).&amp;nbsp; Northwind is not a convention based
database due to use of assignable keys, surrogate keys, and composite keys.&amp;nbsp;
The free version of the AFO code generator only works with convention oriented database
tables and only 5 of the tables in Northwind follow conventions.&amp;nbsp; The readme.txt
file explains why the other tables were not generated.
&lt;/p&gt;
&lt;p&gt;
The commercial version ($30) of the AFO code generator will properly generate code
for all 13 tables in Northwind and is intended to support the entire feature set of
Castle ActiveRecord including things like Composite Keys, which aren't currently supported
by ActiveWriter.
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Now that you have the basic NHibernate Linq infrastructure in place, there are plenty
of Linq examples and sample code available elsewhere.
&lt;/p&gt;
&lt;p&gt;
Enjoy!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.capprime.com/software_development_weblog/aggbug.ashx?id=d404eb9a-57bf-46e0-976a-9f81312ef5a8" /&gt;</description>
      <comments>http://www.capprime.com/software_development_weblog/CommentView,guid,d404eb9a-57bf-46e0-976a-9f81312ef5a8.aspx</comments>
      <category>.NET 3.5</category>
      <category>Castle ActiveRecord</category>
      <category>Knowledge Base</category>
      <category>NHibernate</category>
      <category>SQL Server</category>
      <category>Visual Studio 2008</category>
      <category>.NET 4.0</category>
      <category>AFO</category>
      <category>Visual Studio 2010</category>
    </item>
  </channel>
</rss>