Exclude a property from mapping by code in NH3.2

57 views
Skip to first unread message

Mauro D.

unread,
Dec 13, 2011, 9:45:20 AM12/13/11
to nhusers
I'm using this code for mapping entities:

var inspector = (IModelInspector)new SimpleModelInspector();
ModelMapper mapper = new ModelMapper(inspector);
mapper.Class<Model.Entity>(m =>
{
m.Id(x => x.Id, g => g.Generator(Generators.Native));
m.Table("Entities");
m.Version(x => x.Version, map =>
{ map.Column("NHVersion"); });
m.Property(x => x.Data, map =>
map.Type<NHibernate.Type.BinaryBlobType>());
});

I want to be able to exclude a single property from the mapping.
There's a simple method with this mapping usage or I have to change
style and create my personal inspector inheriting from a base one?

Thanks

fkne...@gmail.com

unread,
Dec 13, 2011, 9:54:27 AM12/13/11
to nhu...@googlegroups.com
Not sure if this is correct, I've used this

Property(x => x.Age,
map =>
{
map.Generated(PropertyGeneration.Never);
});


And my entity property is this

public virtual Int32? Age
{
get
{
if (!DateOfBirth.HasValue) return null;

var now = DateTime.Now;

Int32 age = now.Year - DateOfBirth.Value.Year;

if (now.Month < DateOfBirth.Value.Month)
return age;
if (now.Month > DateOfBirth.Value.Month)
return age--;
if (now.Day < DateOfBirth.Value.Day)
return age--;

return age;
}

set { }
}

This allows you to query on the property

var people = from a in _personRepository.FindAll()
where a.Age > 10
select new PersonViewModel
{
Id = a.Id,
FirstName = a.FirstName,
LastName = a.LastName,
Age = a.Age ?? 0,
City = a.Address.City
};



On , "Mauro D." <mauro...@gmail.com> wrote:
> I'm using this code for mapping entities:
>
>
>
> var inspector = (IModelInspector)new SimpleModelInspector();
>
>            ModelMapper mapper = new ModelMapper(inspector);
>
>            mapper.Class(m =>

>
>            {
>
>                m.Id(x => x.Id, g => g.Generator(Generators.Native));
>
>                m.Table("Entities");
>
>                m.Version(x => x.Version, map =>
>
> { map.Column("NHVersion"); });
>
>                m.Property(x => x.Data, map =>
>
> map.Type());

>
>            });
>
>
>
> I want to be able to exclude a single property from the mapping.
>
> There's a simple method with this mapping usage or I have to change
>
> style and create my personal inspector inheriting from a base one?
>
>
>
> Thanks
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "nhusers" group.
>
> To post to this group, send email to nhu...@googlegroups.com.
>
> To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
>
> For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
>
>
>
>

Mauro D.

unread,
Dec 13, 2011, 10:04:41 AM12/13/11
to nhusers
This doesn't work, property mapping is still generated.

Neo

unread,
Dec 14, 2011, 1:25:37 AM12/14/11
to nhusers
mapper.IsPersistentProperty((mi, declared) => ....);
is your friend...

there is unfortunately not any generic "Exclude", method.

Search the list for more detailed answers, should you need it, as this
has been answered already.
Cheers!

Mauro D.

unread,
Dec 14, 2011, 4:53:21 AM12/14/11
to nhusers
Ok this works, but I had to change ModelMapper to
ConventionModelMapper in order to be able to access
IsPersistentProperty.

That was the problem.

Fabio Maulo

unread,
Dec 15, 2011, 6:32:59 PM12/15/11
to nhu...@googlegroups.com
Using ModelMapper there is no auto-inclusion so you don't need to exclude that is the reason because it is available just in  ConventionModelMapper.

Fabio Maulo

unread,
Dec 15, 2011, 6:37:07 PM12/15/11
to nhu...@googlegroups.com
It is easy Neo.
The idea behind ConvetionModelMapper is that you will extend it using extension-method or, much better, inheriting it.
Well.... the real idea is that you will implement a IModelInspector to then inject it to the ModelMapper.
Implementing a IModelInspector you can expose some methods a Exclude, Persistent (the inverse of exclude for denormalized properties) and some other features... as in ConfORM
Reply all
Reply to author
Forward
0 new messages