C# 如何告诉 Fluent NHibernate 不要映射类属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/907576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:32:59  来源:igfitidea点击:

How to tell Fluent NHibernate not to map a class property

c#nhibernatefluent-nhibernate

提问by modernzombie

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

我有一个在 fluent nhibernate 中映射的类,但我希望映射忽略类属性之一。

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

以下类型不能用作代理: iMasterengine.Data.Model.Calendar:方法 get_HasEvents 应该是虚拟的

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

采纳答案by Owen

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

回答by UpTheCreek

You can just make HasEventsvirtualin the class:

您可以在课堂上进行HasEvents虚拟

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.

如果您正在使用自动映射,您只需要告诉 fluent 输入属性,我认为您不是。