How to tell Fluent NHibernate not to map a class property
Asked Answered
W

3

30

I have a class that is mapped in fluent nhibernate but I want one of the classes properties to be ignored by the mapping.

With class and mapping below I get this error:

The following types may not be used as proxies: iMasterengine.Data.Model.Calendar: method get_HasEvents should be virtual

//my class
public class Calendar : IEntity {
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual string SiteId { get; set; }
    public virtual IList<CalendarEvent> Events { get; set; }
    //ignore this property
    public bool HasEvents { get { return Events.Count > 0; } }
}

//my mapping
public class CalendarMap : ClassMap<Calendar> {
    public CalendarMap() {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.SiteId);
        HasMany(x => x.Events).Inverse();
        //what do I put here to tell nhibernate
        //to ignore my HasEvents property?
    }
}
Wace answered 25/5, 2009 at 18:3 Comment(0)
P
14
map.IgnoreProperty(p => p.What);
Pray answered 25/5, 2009 at 21:58 Comment(6)
Where should that line be placed? I figured it would go into the CalendarMap constructor, but I don't see a map instance available there.Patois
It should be added where you create the configuration. See this link http://wiki.fluentnhibernate.org/Auto_mapping#Ignoring_propertiesBelle
IgnoreProperty() is only for automapping if you are specifying your mappings yourself (i.e. using classmap) then this is not relevant. The poster has included a classmap, and has not said anything about automapping, so I don't see what relevance this has.Striate
I came across this from google, and I am automapping, so even though it may not answer the question, it answered mineMoshemoshell
will i able to change per user basis means,i want to make it available for set of users and hide for some set of users.Palazzo
Where do I add this?Ardehs
S
29

You can just make HasEvents virtual in the class:

public virtual bool HasEvents { get { return Events.Count > 0; } }

You don't need to add anything to the mappings.

You only need to tell fluent to ingore a property if you are using Auto Mapping, which I don't think you are.

Striate answered 7/5, 2010 at 10:58 Comment(1)
Doesn't work for me going through Linq on an old version of NHibernat.eArgument
P
14
map.IgnoreProperty(p => p.What);
Pray answered 25/5, 2009 at 21:58 Comment(6)
Where should that line be placed? I figured it would go into the CalendarMap constructor, but I don't see a map instance available there.Patois
It should be added where you create the configuration. See this link http://wiki.fluentnhibernate.org/Auto_mapping#Ignoring_propertiesBelle
IgnoreProperty() is only for automapping if you are specifying your mappings yourself (i.e. using classmap) then this is not relevant. The poster has included a classmap, and has not said anything about automapping, so I don't see what relevance this has.Striate
I came across this from google, and I am automapping, so even though it may not answer the question, it answered mineMoshemoshell
will i able to change per user basis means,i want to make it available for set of users and hide for some set of users.Palazzo
Where do I add this?Ardehs
D
0

You can also use the [Ignore] attribute in the AutoMapper.Configuration.Annotations namespace. Place it directly above the property declaration.

Decuple answered 7/6, 2023 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.