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.

10
  • So, it's joining to the product when you Get<Demand>? Can you show the sql? Commented Oct 18, 2012 at 20:07
  • 1
    Check out this link, propertyref stackoverflow.com/questions/4888140/… Commented Oct 18, 2012 at 20:09
  • dotjoe it's not actually joining, its doing an entirely separate query to get product, which is even worse really. Commented Oct 18, 2012 at 20:12
  • seems like Lazy loading is not available with property-ref stackoverflow.com/a/1649547/40822 Commented Oct 18, 2012 at 20:17
  • 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. Commented Oct 18, 2012 at 20:31

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.