3

Hey there, I've tried to disable LazyLoad on my collections without luck... The code I've tried so far is:

// Person.cs

public class Person
{
    public virtual int Id { get; private set; }
    public virtual string FirstName { get; set; }
    public virtual IList<Car> Cars { get; set; }

    public Person()
    {
        Cars = new List<Car>();
    }

    public virtual void AddCar(Car car)
    {
        Cars.Add(car);
    }
}

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.Id);
        Map(x => x.FirstName);
        HasMany(x => x.Cars).KeyColumn("PersonId").Cascade.AllDeleteOrphan().Not.LazyLoad();
        Table("Persons");
    }
}

// Car.cs

public class Car
{
    public virtual int Id { get; set; }
    public virtual string CarName { get; set; }
    public virtual Person Person { get; set; }
}

public class CarMap : ClassMap<Car>
{
    public CarMap()
    {
        Id(x => x.Id);
        Map(x => x.CarName);
        HasOne(x => x.Person).Not.LazyLoad();
        Table("Cars");
    }
}

Any suggestions?

Thanks in advance!

3
  • 1
    and how do you know it's not working? Commented Jan 20, 2011 at 21:48
  • @Diego Mijelshon - I've tested it in my MVC app, and it still grab the Car objects for the Person. :) Commented Jan 20, 2011 at 21:48
  • That's what you told it to do. Not.LazyLoad() means "Always load everything", which is usually a bad idea Commented Jan 20, 2011 at 22:12

2 Answers 2

7

Lazy loading means that the cars collection will be loaded only when you access the cars property. So when you turn this off, when loading a person you will always also load all his cars. To see if this "works" for you, you can open up SQL profiler (or grab the trial for NHibernate profiler), and debug your code. When you step through your code you can see if the cars collection is loaded right away, or only when you first access it.

In short, I don't think you've done something wrong, just misunderstood the concept.

Sign up to request clarification or add additional context in comments.

7 Comments

Might be.. How would I then get a Person object without all the Car object thats related to that Person?
@ebb By leaving the default, which is to be lazy.
@Diego Mijelshon - Hmm.. wondering why Lazyloading is called evil then.. always thought that lazyloading meant that it would load all related entities for an Entity. @Doron Yaacoby, you were right.. The NHibernate profiler actually catch 2 SQL statements.. one to get the Person Object and another to get Car Object(s) related to that Person.
@ebb lazy loading means the opposite of that. And is only called "evil" by people who don't understand how to use it.
@ebb, actually eager loading is the one that is evil. Lazy loading is quite nice and charming, as long as you have your session open :)
|
1

If you are using the Load method try the Get instead. Load always returns a proxy and loads lazily iirc. Beware of SELECT N+1 though.

EDIT: After re-reading other comments it probably works as expected. Doron already explained how is lazy loading supposed to work. So if you do NOT want to load all the cars together with a person, then lazy loading should be enabled. Lazy loading is already enabled by default, because if you eagerly load all your collections, this results in a SELECT N+1, as mentioned before. That is, one select for the person and N for cars. Such behavior is usually not desired, hence my warning :) Lazy loading means that collections will be loaded only if explicitly asked for.

1 Comment

Which FluentNH version are you using? It appears there was a problem with HasMany and Not, see support.fluentnhibernate.org/discussions/help/….

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.