3

I am attempting to use NHibernate to generate a model for a very odd database. The tables themselves have primary keys for show only, all the actual relationships are on unique columns. For example, a product table with a product id primary key and a unique product name column. Another table, demand, has a product name column and that defines the relationship. I know this situation isn't ideal but it's out of my control.

At any rate, I was able to use Fluent NHibrenate to map product to demand, but I cannot seem to get the entity to lazy-load.

public class Demand
{
  public virtual DemandId { get; set; }
  public virtual Product { get; set; }
}

public class DemandMap : ClassMap<Demand>
{
  public DemandMap()
  {
    this.Table("Demand");
    this.LazyLoad();
    this.Id(x => x.DemandId);
    this.References(x => x.Product).PropertyRef(x => x.ProductName).LazyLoad();
  }
}

Does anyone have any insight into why lazy loading is not working? I know it is not because I can see the product being fetched along with the demand in the SQL profiler.

user653649
  • 115
  • 1
  • 9
  • So, it's joining to the product when you `Get`? Can you show the sql? – dotjoe Oct 18 '12 at 20:07
  • 1
    Check out this link, propertyref http://stackoverflow.com/questions/4888140/fluent-nhibernate-references-and-propertyref-doing-a-select-with-lazy-load – BlakeH Oct 18 '12 at 20:09
  • dotjoe it's not actually joining, its doing an entirely separate query to get product, which is even worse really. – user653649 Oct 18 '12 at 20:12
  • seems like Lazy loading is not available with property-ref http://stackoverflow.com/a/1649547/40822 – dotjoe Oct 18 '12 at 20:17
  • I wonder if you could map the product name as the NH primary key and remove the propertyref. I suppose that would depend on the id generator though. – dotjoe Oct 18 '12 at 20:19
  • @dotjoe Could you elaborate a bit on that idea? – user653649 Oct 18 '12 at 20:28
  • 1
    For your Product map, you might be able to try `Id(x => x.ProductName).GeneratedBy().Assigned()`...and let the legacy db generate the actual primary key. But, this might only work with an identity id. – dotjoe Oct 18 '12 at 20:31
  • @dotjoe I'll build a trivial database and try that. – user653649 Oct 18 '12 at 20:38
  • @dotjoe that actually worked, i'm a bit worried about unintended consequences however. Lucky for me, the primary keys are NEVER used. I don't think it would be possible to have a mixed scenario and support lazy loading. – user653649 Oct 18 '12 at 21:09
  • When are you depending on LazyLoading, you can only LazyLoad if the session is still open, which means you need to lazyload within your using statement, or make your session per-request. – Phill Jul 17 '13 at 02:28

1 Answers1

1

My idea (Maybe you can try use "HasMany" there is example but you can read something about this):

First class

public class Demand
{
  public virtual int DemandId { get; set; }
  public virtual int Product { get; set; }
  public virtual IEnumerable<NewClass> Name {get; set;} 
}

  this.HasMany(x=> x.Product).Column("Product_id").not.nullable;

Second class

public class NewClass
{
  public virtual Demand Product_id {get; set;}
}

this.References(x => x.Product).Column("product_id).not.nullable
Rafał Developer
  • 2,135
  • 9
  • 40
  • 72