<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn Boland &#187; O/R mapping</title>
	<atom:link href="http://blogs.taiga.nl/martijn/category/or-mapping/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.taiga.nl/martijn</link>
	<description>New adventures in .NET</description>
	<lastBuildDate>Tue, 27 Apr 2010 20:51:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Entity Framework 4 Code-First demo updated to Feature CTP3</title>
		<link>http://blogs.taiga.nl/martijn/2010/04/27/entity-framework-4-code-first-demo-updated-to-feature-ctp3/</link>
		<comments>http://blogs.taiga.nl/martijn/2010/04/27/entity-framework-4-code-first-demo-updated-to-feature-ctp3/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 20:51:13 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[O/R mapping]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/2010/04/27/entity-framework-4-code-first-demo-updated-to-feature-ctp3/</guid>
		<description><![CDATA[The EF 4 code-first demo has been updated to reference the EF4 Feature CTP3 dll at its default location. To run the demo, you have to make sure that the you have it installed. Download the EF 4 Feature CTP3 at http://www.microsoft.com/downloads/details.aspx?FamilyID=af18e652-9ea7-478b-8b41-8424b94e3f58&#38;displayLang=en. Although the demo was originally developed with VS 2010 Beta 2 everything works [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.taiga.nl/martijn/2009/11/22/entity-framework-4-0-a-fresh-start-with-demo-application/">EF 4 code-first demo</a> has been updated to reference the EF4 Feature CTP3 dll at its default location. To run the demo, you have to make sure that the you have it installed. Download the EF 4 Feature CTP3 at <a title="http://www.microsoft.com/downloads/details.aspx?FamilyID=af18e652-9ea7-478b-8b41-8424b94e3f58&amp;displayLang=en" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=af18e652-9ea7-478b-8b41-8424b94e3f58&amp;displayLang=en">http://www.microsoft.com/downloads/details.aspx?FamilyID=af18e652-9ea7-478b-8b41-8424b94e3f58&amp;displayLang=en</a>.</p>
<p>Although the demo was originally developed with VS 2010 Beta 2 everything works perfectly fine on VS 2010 RTM.</p>
<p>You can find the demo sources at github: <a title="http://github.com/martijnboland/EF4-codeonly-demo" href="http://github.com/martijnboland/EF4-codeonly-demo">http://github.com/martijnboland/EF4-codeonly-demo</a> where it’s also possible to download the sources all at once.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2010/04/27/entity-framework-4-code-first-demo-updated-to-feature-ctp3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework 4.0: a fresh start (with demo application)</title>
		<link>http://blogs.taiga.nl/martijn/2009/11/22/entity-framework-4-0-a-fresh-start-with-demo-application/</link>
		<comments>http://blogs.taiga.nl/martijn/2009/11/22/entity-framework-4-0-a-fresh-start-with-demo-application/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 21:31:42 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[O/R mapping]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Castle]]></category>
		<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/2009/11/22/entity-framework-4-0-a-fresh-start-with-demo-application/</guid>
		<description><![CDATA[Edited 2009-11-26: removed EF4 Feature CTP from demo package and added some code examples. So, Entity Framework 1.0 pretty much sucks (compared to alternatives), but I’m glad to see that things have improved a lot in version 4.0 (we’ll call that EF4 from now). To see how the improvements work out, I did a quick [...]]]></description>
			<content:encoded><![CDATA[<p>Edited 2009-11-26: removed EF4 Feature CTP from demo package and added some code examples.</p>
<p>So, <a href="http://blogs.taiga.nl/martijn/2009/09/24/one-year-after-the-entity-framework-vote-of-no-confidence/">Entity Framework 1.0 pretty much sucks</a> (compared to alternatives), but I’m glad to see that things have improved a lot in version 4.0 (we’ll call that EF4 from now). To see how the improvements work out, I did a quick <a href="http://blogs.taiga.nl/martijn/wp-content/uploads/EF4Demo.zip" target="_blank">spike</a> that also gave me the opportunity to test some new ASP.NET MVC 2 features that I might blog about later.</p>
<h3>What’s in it?</h3>
<ul>
<li>Entity Framework 4 with POCO entity objects and <a href="http://blogs.msdn.com/efdesign/archive/2009/10/12/code-only-further-enhancements.aspx" target="_blank">code-only configuration</a> (no edmx);</li>
<li>Data access abstracted via repository interfaces;</li>
<li>ASP.NET 2 MVC for the UI;</li>
<li>Validation with Data Annotations;</li>
<li>Castle Windsor IoC container to wire the various components together;</li>
</ul>
<h3>EF4 POCO</h3>
<p>In EF4, it’s now possible to use entity classes that don’t have to inherit from EntityObject. This allows for better testability and separation of concerns. This is how an entity looks in the demo:</p>
<pre class="brush: csharp;">public class Course
{
    public virtual int Id { get; set; }

    [Required(ErrorMessage="Course title is required")]
    public virtual string Title { get; set; }

    [Required(ErrorMessage = "Price is required")]
    [DataType(DataType.Currency)]
    [Range(10.00, double.MaxValue, ErrorMessage="The minimum price is {1}")]
    public virtual decimal Price { get; set; }

    public virtual ISet&lt;Schedule&gt; Schedules { get; set; }

    public Course()
    {
        this.Schedules = new HashSet&lt;Schedule&gt;();
    }
}</pre>
<p>Note that all properties are marked virtual. This allows EF4 to do some magic with run-time proxy generation to allow lazy loading and change notification.</p>
<p>Also note that we can now easily add validation attributes (in this case from Sytem.ComponentModel.DataAnnotations). We don’t have to use those <a href="http://ayende.com/Blog/archive/2009/05/04/the-buddy-classes-are-drowning-dry.aspx" target="_blank">dreadful ‘buddy classes’</a> anymore to hold the meta-data.</p>
<h3>Code-only configuration</h3>
<p>EF4 now allows model-first design from the VS 2010 designer, but it can still get awkward with a lot of entities. Fortunately, the <a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;FamilyID=13fdfce4-7f92-438f-8058-b5b4041d0f01" target="_blank">EF4 Feature CTP</a> makes it possible <a href="http://blogs.msdn.com/efdesign/archive/2009/10/12/code-only-further-enhancements.aspx" target="_blank">to do everything in code</a>.</p>
<p>So you now just create your POCO entity and add a mapping class that replaces the edmx designer:</p>
<pre class="brush: csharp;">public class CourseMapping : EntityConfiguration&lt;Course&gt;
{
    public CourseMapping()
    {
        HasKey(c =&gt; c.Id);
        Property(c =&gt; c.Id).IsIdentity();
        MapSingleType(c =&gt; new
        {
            courseid = c.Id,
            title = c.Title,
            price = c.Price
        }).ToTable("course");
        Property(c =&gt; c.Price).HasStoreType("money").HasPrecision(19, 4);
    }
}</pre>
<p>Looks remarkably similar to <a href="http://wiki.fluentnhibernate.org/Main_Page" target="_blank">Fluent NHibernate</a>, doesn’t it? <img src='http://blogs.taiga.nl/martijn/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I’m not sure if this is the most optimal way to define the mapping, but it works. In the demo, there is a class CoursesContextBuilder that wraps the ContextBuilder from the EF4 Feature CTP where the mappings are added. It also serves as a factory for new ObjectContext instances () :</p>
<pre class="brush: csharp;">public class CoursesContextBuilder
{
    private ContextBuilder&lt;ObjectContext&gt; _builder;
    private string _defaultConnectionString;

    public CoursesContextBuilder(string defaultConnectionString)
    {
        this._defaultConnectionString = defaultConnectionString;
        this._builder = new ContextBuilder&lt;ObjectContext&gt;();
        ConfigureMappings();
    }

    private void ConfigureMappings()
    {
        _builder.Configurations.Add(new TeacherMapping());
        _builder.Configurations.Add(new CourseMapping());
        _builder.Configurations.Add(new ScheduleMapping());
    }

    public ObjectContext GetContext()
    {
        return GetContext(_defaultConnectionString);
    }

    public ObjectContext GetContext(string connectionString)
    {
        var context = _builder.Create(GetConnection(connectionString));
        context.ContextOptions.LazyLoadingEnabled = true;
        return context;
    }

    private DbConnection GetConnection(string connectionString)
    {
        // Hardcoded to SqlConnection for this demo.
        return new SqlConnection(connectionString);
    }
}</pre>
<p>That’s all we need to get EF4 working. In theory, we can now do everything with the POCO classes and the ObjectContext that comes from the CoursesContextBuilder, but of course if we would use it this way, we are directly tied to EF again and there goes away our testability.</p>
<h3>Repository implementation</h3>
<p>To make sure our application code isn’t tied to EF directly, data access goes through Repository interfaces. In the demo app you can find a generic IRepository&lt;T&gt; interface that is implemented by an EfRepository&lt;T&gt; class. The EfRepository implementation uses the EF ObjectContext to perform queries and so on. Note that we added an extra IContextManager interface that the EfRepository depends on. The IContextManager is responsible for managing the lifetime of the ObjectContext that is obtained from the ContextBuilder. This way, the repository implementations don’t have to worry about creating and disposing ObjectContext instances. It’s just always always available.All specific Repository interfaces inherit from IRepository&lt;T&gt; and the specific implementations inherit from EfRepository&lt;T&gt;.</p>
<pre class="brush: csharp;">public interface ICourseRepository : IRepository&lt;Course&gt;
{
    void DeleteCourseWithSchedule(Course course);
    Ef4Poco.Domain.Course GetCourseWithSchedulesAndTeachers(int courseId);
    void RemoveScheduleFromCourse(Schedule schedule, Course course);
}</pre>
<pre class="brush: csharp;">/// &lt;summary&gt;
/// Course-specific repository.
/// &lt;/summary&gt;
public class CourseRepository : EfRepository&lt;Course&gt;, ICourseRepository
{
    public CourseRepository(IContextManager contextManager)
        : base(contextManager)
    { }

    public Course GetCourseWithSchedulesAndTeachers(int courseId)
    {
        // Wouldn't it be nice to have strong-typed includes?
        var query = from c in ObjectSet.Include("Schedules").Include("Schedules.Teacher")
                    where c.Id == courseId
                    select c;
        return query.Single();
    }

    public void RemoveScheduleFromCourse(Schedule schedule, Course course)
    {
        course.Schedules.Remove(schedule);
        CurrentObjectContext.DeleteObject(schedule);
        CurrentObjectContext.SaveChanges();
    }

    public void DeleteCourseWithSchedule(Course course)
    {
        // Howto configure cascade delete via code? This is a little cumbersome.
        var schedules = new List&lt;Schedule&gt;(course.Schedules);
        foreach (var schedule in schedules)
        {
            CurrentObjectContext.DeleteObject(schedule);
        }
        CurrentObjectContext.DeleteObject(course);
        CurrentObjectContext.SaveChanges();
    }
}</pre>
<h3>Consuming the data access interfaces</h3>
<p>Now we have our data access interfaces in place, it’s time for consuming. This is plain simple. Below is an example of how a controller in the ASP.NET MVC app in the demo uses the interfaces:</p>
<pre class="brush: csharp;">public class TeachersController : Controller
{
    private ITeacherRepository _teacherRepository;

    public TeachersController(ITeacherRepository teacherRepository)
    {
        _teacherRepository = teacherRepository;
    }

    public ActionResult Index()
    {
        var teachers = _teacherRepository.Find().OrderBy(t =&gt; t.Name);
        return View(teachers);
    }

    [...snip other methods]
}</pre>
<h3>The demo app</h3>
<p>You can<a href="http://blogs.taiga.nl/martijn/wp-content/uploads/EF4Demo.zip" target="_blank"> download the demo</a> to see what’s possible. It’s by no means a best practices example. Just a spike to test out various new technologies.</p>
<p>To run the demo, you’ll need Visual Studio 2010 Beta 2, <a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;FamilyID=13fdfce4-7f92-438f-8058-b5b4041d0f01" target="_blank">the latest EF4 Feature CTP</a> and SQL Server (Express).</p>
<p>Both, the web and the test project have a connection string in the config file that defaults to the .\SQLEXPRESS instance. Change that if you want to use a different instance. The database name doesn’t matter because a new database will be created the first time you run the application. Note that the reference to the EF4 feature CTP (Microsoft.Data.Entity.CTP.dll) points to C:\Program Files (x86)\Microsoft ADO.NET Entity Framework Feature CTP2\Binaries. On 32 bit machines, you’ll probably have to change that to C:\Program Files\…</p>
<h3>Some observations</h3>
<ul>
<li>Entity Framework 4.0 is much, much better than version 1.0, especially for a model-first approach;</li>
<li>It’s not on par yet with <a href="http://nhforge.org" target="_blank">NHibernate</a> feature-wise (for example, I really miss cascade settings). Given the choice, I’d still opt for NHibernate, but it’s not a bad product anymore and I&#8217;d prefer it over LINQ to SQL at this time.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2009/11/22/entity-framework-4-0-a-fresh-start-with-demo-application/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>One year after the Entity Framework Vote of No Confidence&#8230;</title>
		<link>http://blogs.taiga.nl/martijn/2009/09/24/one-year-after-the-entity-framework-vote-of-no-confidence/</link>
		<comments>http://blogs.taiga.nl/martijn/2009/09/24/one-year-after-the-entity-framework-vote-of-no-confidence/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 15:52:07 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[O/R mapping]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/2009/09/24/one-year-after-the-entity-framework-vote-of-no-confidence/</guid>
		<description><![CDATA[About a year ago, a few people (called by some as ‘The NHibernate Mafia’) wrote a vote of no confidence against the Microsoft ADO.NET Entity Framework (EF), mainly because a big influential company like Microsoft was releasing an inferior tool set. See the text of the petition for the detailed reasoning. My initial thoughts were [...]]]></description>
			<content:encoded><![CDATA[<p>About a year ago, a few people (called by some as ‘The NHibernate Mafia’) wrote a <a href="http://efvote.wufoo.com/forms/ado-net-entity-framework-vote-of-no-confidence/" target="_blank">vote of no confidence</a> against the Microsoft ADO.NET Entity Framework (EF), mainly because a big influential company like Microsoft was releasing an inferior tool set. See the <a href="http://efvote.wufoo.com/forms/ado-net-entity-framework-vote-of-no-confidence/" target="_blank">text of the petition</a> for the detailed reasoning. </p>
<p>My initial thoughts were ‘why worry, there are plenty alternatives like <a href="http://nhforge.org" target="_blank">NHibernate</a>, <a href="http://www.llblgen.com" target="_blank">LLBLGen Pro</a> or <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx" target="_blank">LINQ to SQL</a>’ and although I agree with the content of the vote of no confidence, I decided not to sign. That was until realized that as a contractor, you often have to confirm to the technology that the customer has adopted, so it’s to be expected that a lot of future projects have EF as their O/R mapping layer because ‘Microsoft says it’s the way to do data access’. I signed and yes, it’s the selfish me that made me do it.</p>
<p>So now we’re one year later and I just started on a new contract where EF is being used and as I already feared one year ago things are not pretty. The previous developers seem to have struggled a lot, which resulted in poor performance and lots of workarounds.</p>
<p>And you know what: I don’t blame EF itself or the developers but only Microsoft marketing for labeling the Entity Framework as the ‘preferred magic way to do data access’ and all the surrounding blah of future products that will build on EF so people blindly take it as their weapon of choice.</p>
<p>Sorry for this somewhat negative post, but I had to write this off.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2009/09/24/one-year-after-the-entity-framework-vote-of-no-confidence/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>NHibernate criteria queries across multiple many-many associations</title>
		<link>http://blogs.taiga.nl/martijn/2008/11/20/nhibernate-criteria-queries-across-multiple-many-many-associations/</link>
		<comments>http://blogs.taiga.nl/martijn/2008/11/20/nhibernate-criteria-queries-across-multiple-many-many-associations/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 11:31:07 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[O/R mapping]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/archive/2008/11/20/nhibernate-criteria-queries-across-multiple-many-many-associations.aspx</guid>
		<description><![CDATA[Recently, I ran into an issue with NHibernate Criteria queries. The scenario is the following: User has a many-many association with Role and Role has a many-many association with Site. I simply wanted all users that belong to a given site (and a whole slew of other optional parameters, therefore the Criteria query). With hql [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I ran into an issue with NHibernate Criteria queries. The scenario is the following:</p>
<p><a href="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/NHibernatecriteriaqueriesacrossmultiplem_AF7F/double-many-many_4.png"><img style="border: 0pt none;" src="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/NHibernatecriteriaqueriesacrossmultiplem_AF7F/double-many-many_thumb_1.png" border="0" alt="double-many-many" width="642" height="108" /></a></p>
<p>User has a many-many association with Role and Role has a many-many association with Site. I simply wanted all users that belong to a given site (and a whole slew of other optional parameters, therefore the Criteria query).</p>
<p>With hql this is simple:</p>
<div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">select distinct u from User u join u.Roles r join r.Sites s <span style="color: #0000ff">where</span> s.Id = :siteId</pre>
</div>
</div>
<p>With a criteria query, it appeared a little bit harder. First I tried this:</p>
<div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">ICriteria crit = session.CreateCriteria(<span style="color: #0000ff">typeof</span>(User))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">    .CreateCriteria(<span style="color: #006080">"Roles"</span>) <span style="color: #008000">// traverse into Roles</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">        .CreateCriteria(<span style="color: #006080">"Sites"</span>) <span style="color: #008000">// traverse into Sites</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">            .Add(Expression.Eq(<span style="color: #006080">"Id"</span>, siteId);</pre>
</div>
</div>
<p>Note the absence of the <em>distinct</em> keyword anywhere in the query. A nice Cartesian Product was the result due to the joins on the linking tables. Some googling showed that we can get distinct results with Criteria queries by applying the DistinctRootEntityResultTransformer via Criteria.SetResultTransformer() but that all happens in memory and not in the database query (call me old-fashioned, but I&#8217;d like my database results properly filtered <img src='http://blogs.taiga.nl/martijn/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
<p>Finally, I found the solution in using <a href="http://davybrion.com/blog/2008/10/querying-with-nhibernate/" target="_blank">subqueries</a>:</p>
<div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">ICriteria crit = session.CreateCriteria(<span style="color: #0000ff">typeof</span>(User));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">DetachedCriteria roleIdsForSite = DetachedCriteria.For(<span style="color: #0000ff">typeof</span> (Role))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">    .SetProjection(Projections.Property(<span style="color: #006080">"Id"</span>))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">    .CreateCriteria(<span style="color: #006080">"Sites"</span>, <span style="color: #006080">"site"</span>)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">    .Add(Expression.Eq(<span style="color: #006080">"site.Id"</span>, siteId.Value));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">DetachedCriteria userIdsForRoles = DetachedCriteria.For(<span style="color: #0000ff">typeof</span>(User))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">    .SetProjection(Projections.Distinct(Projections.Property(<span style="color: #006080">"Id"</span>)))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">    .CreateCriteria(<span style="color: #006080">"Roles"</span>)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">        .Add(Subqueries.PropertyIn(<span style="color: #006080">"Id"</span>, roleIdsForSite));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">crit.Add(Subqueries.PropertyIn(<span style="color: #006080">"Id"</span>, userIdsForRoles));</pre>
</div>
</div>
<p>Yes, that&#8217;s an insane amount of code to do something that simple, but it works and the generated SQL is highly efficient <img src='http://blogs.taiga.nl/martijn/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Notice the <em>distinct</em> projection in the second DetachedCriteria (userIdsForRoles).</p>
<p>I&#8217;d really appreciate it if somebody has any suggestions for improvement.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2008/11/20/nhibernate-criteria-queries-across-multiple-many-many-associations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
