modernzombie
modernzombie

Reputation: 2049

How to tell Fluent NHibernate not to map a class property

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?
    }
}

Upvotes: 30

Views: 26361

Answers (3)

Erik Knowles
Erik Knowles

Reputation: 1007

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

Upvotes: 0

Owen
Owen

Reputation: 7098

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

Upvotes: 14

UpTheCreek
UpTheCreek

Reputation: 32401

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.

Upvotes: 29

Related Questions