views:

1015

answers:

3

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?
    }
}
+1  A: 

map.IgnoreProperty(p => p.What);

Owen
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
A: 

I think you can just make HasEvents virtual:

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

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

UpTheCreek
A: 

Is this a defect in nHibernate? Why should it be necessary to have HasEvents be virtual in the first place, since it's not being mapped? Unless it's possible the compiler might try inline the code in the HasEvenets and convert Events into the underlying _events field, but I would think the fact that Events is virtual should prevent the compiler from doing that.

Rich