Note that there are some explanatory texts on larger screens.

plurals
  1. POfluent nhibernate one to many mapping
    primarykey
    data
    text
    <p>I am trying to figure out what I thought was just a simple one to many mapping using fluent Nhibernate. I hoping someone can point me to the right directory to achieve this one to many relations I have an articles table and a categories table Many Articles can only belong to one Category Now my Categores table has 4 Categories and Articles has one article associated with cateory1</p> <p>here is my setup. </p> <pre><code>using FluentNHibernate.Mapping; using System.Collections; using System.Collections.Generic; namespace FluentMapping { public class Article { public virtual int Id { get; private set; } public virtual string Title { get; set; } public virtual Category Category{get;set;} } public class Category { public virtual int Id { get; private set; } public virtual string Description { get; set; } public virtual IList&lt;Article&gt; Articles { get; set; } public Category() { Articles=new List&lt;Article&gt;(); } public virtual void AddArticle(Article article) { article.Category = this; Articles.Add(article); } public virtual void RemoveArticle(Article article) { Articles.Remove(article); } } public class ArticleMap:ClassMap&lt;Article&gt; { public ArticleMap() { Table("Articles"); Id(x =&gt; x.Id).GeneratedBy.Identity(); Map(x =&gt; x.Title); References(x =&gt; x.Category).Column("CategoryId").LazyLoad(); } public class CategoryMap:ClassMap&lt;Category&gt; { public CategoryMap() { Table("Categories"); Id(x =&gt; x.Id).GeneratedBy.Identity(); Map(x =&gt; x.Description); HasMany(x =&gt; x.Articles).KeyColumn("CategoryId").Fetch.Join(); } } } } </code></pre> <p>if I run this test</p> <pre><code>[Fact] public void Can_Get_Categories() { using (var session = SessionManager.Instance.Current) { using (var transaction = session.BeginTransaction()) { var categories = session.CreateCriteria(typeof(Category)) //.CreateCriteria("Articles").Add(NHibernate.Criterion.Restrictions.EqProperty("Category", "Id")) .AddOrder(Order.Asc("Description")) .List&lt;Category&gt;(); } } } </code></pre> <p>I am getting 7 Categories due to Left outer join used by Nhibernate any idea what I am doing wrong in here? Thanks [Solution] After a couple of hours reading nhibernate docs I here is what I came up with</p> <pre><code>var criteria = session.CreateCriteria(typeof (Category)); criteria.AddOrder(Order.Asc("Description")); criteria.SetResultTransformer(new DistinctRootEntityResultTransformer()); var cats1 = criteria.List&lt;Category&gt;(); </code></pre> <p>Using Nhibernate linq provider </p> <pre><code> var linq = session.Linq&lt;Category&gt;(); linq.QueryOptions.RegisterCustomAction(c =&gt; c.SetResultTransformer(new DistinctRootEntityResultTransformer())); var cats2 = linq.ToList(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload