1

I'm using mapping by code in NHibernate. I got a class with several properties. One of them is not related to any columns in DB but still has getter and setter.

I use ConventionModelMapper not ModelMapper. The first one assumes that all properties are mapped.

How i can tell to NHibernate to ignore it?

Vasiliy Shiryaev
  • 600
  • 4
  • 12
  • Without wishing to avoid the question, wouldn't it better to not have unmapped properties in your NHibernate entities? Perhaps a viewmodel would be better suited as a home for your custom properties? – SpaceBison Oct 05 '11 at 08:40
  • Actully it is an wrapper around one of the mapped properties. I have base class "Notification" with property "Entity" which is mapped as Any. And i have subclass "NewOrderNotification" with property "Order" which is just more typed wrapper around "Entity" – Vasiliy Shiryaev Oct 05 '11 at 09:19
  • check [this][1] question [1]: http://stackoverflow.com/questions/907576/how-to-tell-fluent-nhibernate-not-to-map-a-class-property – wiero Oct 05 '11 at 09:43
  • Thx for link but i don't use Fluent NHibernate. I use embedded feature of NHibernate itself called mapping by code. – Vasiliy Shiryaev Oct 05 '11 at 10:06
  • @VasiliyShiryaev also see [this question](http://stackoverflow.com/questions/7830096/how-to-ignore-mapping-of-property-using-mapping-by-code-conventions/7843223#7843223) for an alternative solution – Newbie Oct 21 '11 at 01:34
  • Anothere solutions can be found here http://stackoverflow.com/questions/7830096/how-to-ignore-mapping-of-property-using-mapping-by-code-conventions/10262881#10262881 – Vasiliy Shiryaev Apr 21 '12 at 20:49

3 Answers3

5

I find it easier to just create an attribute, attach that attribute to the property, and check for it in the mapper.IsPersistentProperty method. Something like this:

class IngnoreAttribute : Attribute
{
}

class Foo
{
    [Ignore]
    public virtual string Bar { get; set; }
}

mapper.IsPersistentProperty((mi, declared) => mi.GetCustomAttribute<IgnoreAttribute>() == null);

This way, I don't have to keep a list of properties to be ignored at the mapping codes.

mxmissile
  • 11,464
  • 3
  • 53
  • 79
Ikhwan
  • 343
  • 3
  • 11
3

Why not map the properties you want and leave the ones not needed to be mapped

check this

You can manage the persistence of ConventionModelMapper as following:

mapper.BeforeMapProperty += (mi, propertyPath, map) =>
{
    // Your code here using mi, propertyPath, and map to decide if you want to skip the property .. can check for property name and entity name if you want to ignore it
};

A better answer would be:

mapper.IsPersistentProperty((mi, declared) =>
                                             {
                                                 if (mi.DeclaringType == typeof (YourType) && mi.Name == "PropertyNameToIgnore")
                                                     return false;
                                                 return true;
                                             });
Mohamed Abed
  • 5,025
  • 22
  • 31
  • It is true but only for xml. Automapping assumes that all properties are mapped by default. Moreover i don't really use ClassMapping<> for every entity. I just have set of conventions and let NHibernate map all my classes automatically. – Vasiliy Shiryaev Oct 05 '11 at 10:19
  • you use FluentNhibernate ?? if so then check this: http://wiki.fluentnhibernate.org/Auto_mapping .Override(map => { map.IgnoreProperty(x => x.YourProperty); }); – Mohamed Abed Oct 05 '11 at 10:28
  • 1
    *true for xml and ModelMapper. But i use ConventionModelMapper. No, it is not Fluent NHibernate. – Vasiliy Shiryaev Oct 05 '11 at 10:29
  • Wast'n able to find how to use mapper.BeforeMapProperty to decide whether map property or not. According to that [post](http://groups.google.com/group/nhusers/browse_thread/thread/9a69095af13a55f4/1900640725ba032c?lnk=gst&q=mapping+by+code&pli=1) there is no easy way to ignore property. – Vasiliy Shiryaev Oct 06 '11 at 11:40
  • Edited my answer for a better solution – Mohamed Abed Oct 20 '11 at 15:43
  • Yes, thats the way to ignore column, thanks. However there is one addition - if you left code as is then ALL properties of ALL classes would be counted as persisted( even read only for example ). So instead of returning true the code should fall back to default behavior as described here http://groups.google.com/group/nhusers/msg/be4e084cf7cdfed9 – Vasiliy Shiryaev Nov 10 '11 at 07:24
0

If you do not mention the property that should be ignored in your NHibernate mapping, NHibernate will ignore it.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154