Search code examples
nhibernatefluent-nhibernate

FluentNHibernate: CamelCaseField vs ReadOnlyPropertyThroughCamelCaseField


Given the following class:

public class User {
  private IList<ActivationToken> _activationTokens;
  public virtual IReadOnlyList<ActivationToken> ActivationTokens => _activationTokens.AsReadOnly();
}

what should the correct FluentNHibernate mapping be and why?

HasMany( x => x.ActivationTokens ).Access.CamelCaseField( Prefix.Underscore );

or

HasMany( x => x.ActivationTokens ).Access.ReadOnlyPropertyThroughCamelCaseField( Prefix.Underscore );

Tnx


Solution

  • The correct mapping is:

    HasMany(x => x.ActivationTokens) .Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore); enter code here

    Reason:

    • Your ActivationTokens property is read-only, so NHibernate needs to access the backing field _activationTokens.
    • ReadOnlyPropertyThroughCamelCaseField tells NHibernate to use the _activationTokens field but respect that the property is read-only.