Using Fluent NHibernate AutoPersistenceModel but with eager loading in one object

I am using Fluent NHibernate to bind my objects automatically.

This is the code I am using to auto match:

new AutoPersistenceModel()
  .AddEntityAssembly(Assembly.GetAssembly(typeof(Entity)))
  .Where(type => type.Namespace.Contains("Domain") && type.BaseType != null && type.BaseType.Name.StartsWith("DomainEntity") && type.BaseType.IsGenericType == true)
  .WithSetup(s => s.IsBaseType = (type => type.Name.StartsWith("DomainEntity") && type.IsGenericType == true))
  .ConventionDiscovery.Add(
      ConventionBuilder.Id.Always(x => x.GeneratedBy.Increment())
);

      

This works great. But now I need to have Eager Loading in one object of my domain. Found this answer . But when I add a line .ForTypesThatDeriveFrom<IEagerLoading>(map => map.Not.LazyLoad())

to the code and run it, I get the following exception:

  • Error while trying to create a mapping document for IEagerLoading

Note that I am using the ( IEagerLoading

) interface to mark the objects I want to receive.

Can anyone help how to do this? Remember that I want to keep the auto-matching functionality.

thanks

0


source to share


1 answer


The problem you are having is that it is a ForTypesThatDeriveFrom<T>

little misleading and what it really means ForMappingsOf<T>

, so it tries to find ClassMap<IEagerLoading>

, which obviously does not exist.

I believe you can handle this with IClassConvention

. This is off the table, but should work:



public class EagerLoadingConvention : IClassConvention
{
  public bool Accept(IClassMap target)
  {
    return GetType().GetInterfaces().Contains(typeof(IEagerLoading));
  }

  public void Apply(IClassMap target)
  {
    target.Not.LazyLoad();
  }
}

      

+3


source







All Articles