I have a class model that looks like this:
public class TheSystem
{
public virtual Guid Id { get; protected set; }
public virtual ICollection<User> Users { get; protected set; }
}
public class User
{
public virtual Guid Id { get; protected set; }
public virtual string Username { get; set; }
...
}
The mappings
public class TheSystemMap : ClassMap<TheSystem>
{
public TheSystemMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
HasMany(x => x.Users).Cascade.AllDeleteOrphan().ExtraLazyLoad().Cache.ReadWrite().IncludeNonLazy();
Cache.ReadOnly();
}
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Username).Not.Nullable();
...
Cache.ReadOnly();
}
}
The problem arrives when I want to add a new user to the system. Because of I'm referencing the Users
collection, NHibernate loads all the instances (which I don't want, because I just want to insert the single user).
...
theSystem.Users.Add(new User("aUser"));
...
The ExtraLazyLoad()
option is working as expected when I try to get the item's count (The code next produces to query only for the count)
...
var count = theSystem.Users.Count;
...
I've also try adding .AsBag()
option with the same result.
I'm missing something in the mapping or is a situation that can't be solved the regular ways?