4
votes

We recently upgraded our Automapped FNH / NH project to NH 3.2, and are running into a lot of problems.

We have a lot of properties in our object model that only have a "getter", e.g.

    public virtual float Polydispersity
    {
        get { return MeasurementRun.PolyDispersity; }
    }

This used to work fine, but now we get "Could not find a setter for property" errors.

This was hard to find, because for some reason the automapping was silently failing to create the DB tables, leading to "no persister" errors when saving to the DB - see this for details.

Is this because LinFu is now built into NH? I know private setters no longer work, and this seems to be a similar problem.

We used to use Castle, and I really like that NH doesn't require all those other DLLs now, but not if it breaks a big part of our object model.

Any suggestions on how to get around this?

Stack Trace

FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

---> NHibernate.PropertyNotFoundException: Could not find a setter for property 'Polydispersity' in class 'BI_OverlordDlsAppCore.OfeDlsMeasurement' at NHibernate.Properties.BasicPropertyAccessor.GetSetter(Type type, String propertyName) at NHibernate.Mapping.Property.GetSetter(Type clazz) at NHibernate.Tuple.Entity.PocoEntityTuplizer.BuildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) at NHibernate.Tuple.Entity.AbstractEntityTuplizer..ctor(EntityMetamodel entityMetamodel, PersistentClass mappingInfo) at NHibernate.Tuple.Entity.PocoEntityTuplizer..ctor(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) at NHibernate.Tuple.Entity.EntityEntityModeToTuplizerMapping..ctor(PersistentClass mappedEntity, EntityMetamodel em) at NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass, ISessionFactoryImplementor sessionFactory) at NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory) at NHibernate.Persister.Entity.JoinedSubclassEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping) at NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg) at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) at NHibernate.Cfg.Configuration.BuildSessionFactory() at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() --- End of inner exception stack trace --- at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() at BI_OverlordPackageCore.OfeDatabase.CreateSqliteSessionFactory() in C:\Bicw_Dev\Bic.Net\Private Projects\NHibernate\FNH 1.3 Mapping Bug\OfeDatabase.cs:line 192 at BI_OverlordPackageCore.OfeDatabase.GetMainSession() in C:\Bicw_Dev\Bic.Net\Private Projects\NHibernate\FNH 1.3 Mapping Bug\OfeDatabase.cs:line 147 at BI_OverlordPackageCore.OfeDatabase.Initialize(Assembly[] assemblies) in C:\Bicw_Dev\Bic.Net\Private Projects\NHibernate\FNH 1.3 Mapping Bug\OfeDatabase.cs:line 305 at FNH_1._3_Mapping_Bug.Form1.button1_Click(Object sender, EventArgs e) in C:\Bicw_Dev\Bic.Net\Private Projects\NHibernate\FNH 1.3 Mapping Bug\Form1.cs:line 23 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

1
I'm using NHibernate 3.2.0.GA with many get-only-properties and it works fine. Please post the full exception message and stack trace.cremor
@cremor - I added the stack traceTom Bushell

1 Answers

7
votes

You should not map readonly properties.

- Solution 1

    var map = AutoMap.AssemblyOf<Dummy>()
         .OverrideAll(x => x.IgnoreProperties(property => !property.CanWrite));

- Solution 2

internal class MyMappingConfiguration : DefaultAutomappingConfiguration
{
        public override bool ShouldMap(Member member)
        {
            return base.ShouldMap(member) && member.CanWrite;
        }
}

... Then use your conf :

var map = AutoMap.AssemblyOf<Dummy>(new MyMappingConfiguration());

Edit : Added Tom Bushell's suggestion