0

Is there a way to use Fetch with collection that is private?

This is what i have for code:

public class Owner
{
    private ICollection<Cat> _cats = new List<Cat>();

    public virtual int Id { get; set; }

    public virtual IEnumerable<Cat> Cats { get { return _cats; } }

    public virtual void AddCat(Cat cat) { ... }
}

public class Cat
{
    public virtual int Id { get; set; } 

    public virtual string Name { get; set; }

    public virtual Owner Owner { get; set; }
}

Most of the time, I want to lazy load the Cats collection, but sometimes I don't. I want to use Fetch in a Linq query to eager load it. I currently get a "could not resolve property: Cats..." exception. I am assuming I get this because I have a Set("_cats", ...) in my ClassMapping, and its looking for the property Cats to be mapped. Is there a way to get Fetch to work with the private collection of Cats?

mekansm
  • 73
  • 5

2 Answers2

0

NHibernate generates proxies from your objects, when they are loaded from database, so the properties you want to map must be virtual. You should make your private cats collection protected virtual and try again. I only mapped properties with a protected setter and a public getter, but this solution may be suitable with full protected properties, too.

rumpelstiefel
  • 466
  • 3
  • 5
0

You need to specify nosetter access strategy in property mapping.

Take a look at this answer for details: Domain Model with Nhibernate design issue

Community
  • 1
  • 1
Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47