FluentNHibernate - 自动忽略属性

2 fluent-nhibernate

我有一个基类,包含一个名为IsDirty的属性.这用于域模型,不是数据库表中的列.

使用自动化时,流畅的nhibernate尝试将此列添加到表中.解决这个问题的一种方法是进入.ForTypesThatDeriveFrom<Address>(p => p.IgnoreProperty(x => x.IsDirty))自动化设置.

问题是,我的所有实体都会这样做,有没有办法说明这一点,而不必为每个实体添加这一行?如果我放.ForTypesThatDeriveFrom<Entity>(p => p.IgnoreProperty(x => x.IsDirty)),然后我尝试将实体转换为地址时出错.

我也将实体设置为基本类型.

在此先感谢,JT

ung*_*ood 8

您创建一个派生自的类DefaultAutomappingConfiguration并覆盖该ShouldMap(Member member)方法.

像这样:

public class AutoMapConfiguration : DefaultAutomappingConfiguration
{
    private static readonly IList<string> IgnoredMembers = new List<string> {"IsNew", "IsDeleted"};

    public override bool ShouldMap(Member member)
    {
        return base.ShouldMap(member) && !IgnoredMembers.Contains(member.Name);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后您的流畅配置将如下所示:

Fluently.Configure()
    // Database settings
    .Mappings(m => {
        m.AutoMappings.Add(yourMappingAssembly, new AutoMapConfiguration())
    });
Run Code Online (Sandbox Code Playgroud)