29

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?
    }
}
modernzombie
  • 1,987
  • 7
  • 25
  • 44

3 Answers3

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.

UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
14
map.IgnoreProperty(p => p.What);
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Owen
  • 6,992
  • 7
  • 44
  • 77
  • 11
    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. – ddc0660 Oct 19 '09 at 15:07
  • It should be added where you create the configuration. See this link [http://wiki.fluentnhibernate.org/Auto_mapping#Ignoring_properties](http://wiki.fluentnhibernate.org/Auto_mapping#Ignoring_properties) – Rajeesh Feb 16 '11 at 05:27
  • 19
    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. – UpTheCreek Jun 01 '11 at 08:06
  • 1
    I came across this from google, and I am automapping, so even though it may not answer the question, it answered mine – harriyott Aug 23 '12 at 15:56
  • 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. – user1220402 Apr 17 '15 at 05:53
  • Where do I add this? – rabejens Jan 30 '18 at 08:18
0

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

Erik Knowles
  • 999
  • 7
  • 15