Mapping by code conventions in NH 3.2 & .Version(...)

44 views
Skip to first unread message

Alexander Kot

unread,
Aug 15, 2011, 9:28:06 AM8/15/11
to nhu...@googlegroups.com
Hello all

I am playing with mapping by code convention in 3.2
I would like to implement next:
If there is int Ver {...} property in class use it for versioning.

I do next:
ConventionModelMapper m_mapper = new ConventionModelMapper();
........
m_mapper.BeforeMapClass +=
           (mi, t, map) =>
               {
                   map.Id( .....);
                  
                   map.Version(t.GetProperty("Ver") , vmap => vmap.Column("VER"));
               };
That is not enough
when i do OpenSession there is exception
"Duplicate property mapping of Ver found in WindowsFormsApplication3.MyEntity......."
So despite map.Version(...) call, Ver property is mapped also as simple property by core ( i see it in call to m_mapper.BeforeMapProperty )

If add
m_mapper.IsPersistentProperty((m, declared) => (m.Name != "Ver"));
All will work fine

Is that bug or feature?

Fabio Maulo

unread,
Aug 15, 2011, 9:47:06 AM8/15/11
to nhu...@googlegroups.com
What you have to do in the ModelMapper is another thing and I'm sure you already know what you have to do... just have a look to others IsXYZ ;)

Alexander Kot

unread,
Aug 15, 2011, 10:25:30 AM8/15/11
to nhu...@googlegroups.com
Yes
it seems that i have already found .IsVersion - it has same effect

But why i need to specify IsVersion explicitly?

I see that BeforeMapClass is called before mapping properties of this class :), so info about which property is version already accessible by core at the moment of property mapping.

For PK it is enough specify  map.Id( .....);  in m_mapper.BeforeMapClass  and there is no need to specify IsPersistentId in mapper.
(1 declaration statement)

It is also enough to specify
           m_mapper.Class<MyEntity>(map =>
            {
                map.Version(x => x.Ver, ....);   //(1 declaration statement per property)
            });

But 2 declarations in my initial example....
Reply all
Reply to author
Forward
0 new messages