FluentNHibernate - 自动映射忽略属性 [英] FluentNHibernate - Automapping ignore property

查看:559
本文介绍了FluentNHibernate - 自动映射忽略属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



使用自动映射时,流利的nhibernate会尝试将此列添加到表中。一种解决这个问题的方法是在automapping设置中放置 .ForTypesThatDeriveFrom< Address>(p => p.IgnoreProperty(x => x.IsDirty)) / p>

问题是,我的所有实体都会这样做,有没有办法在不必为每个实体添加此行的情况下声明?如果我把 .ForTypesThatDeriveFrom< Entity>(p => p.IgnoreProperty(x => x.IsDirty)),那么当试图将Entity转换为地址。



我也有实体设置为基本类型。

在此先感谢,
JT创建一个从 DefaultAutomappingConfiguration 派生的类,然后重载





pre $ public class AutoMapConfiguration:DefaultAutomappingConfiguration
{
private static readonly IList< string> IgnoredMembers =新列表< string> {IsNew,IsDeleted};

public override bool ShouldMap(成员)
{
return base.ShouldMap(member)&& !IgnoredMembers.Contains(member.Name);






$ p

然后你的流畅配置看起来像这样: / b>

 流利的.Configure()
//数据库设置
.Mappings(m => {
m.AutoMappings.Add(yourMappingAssembly,new AutoMapConfiguration())
});


I have a base class that contains a property called IsDirty. This is used for the domain model and is not a column in the database table.

When using automapping, fluent nhibernate tries to add this column to the table. A way to fix this is to put .ForTypesThatDeriveFrom<Address>(p => p.IgnoreProperty(x => x.IsDirty)) in the automapping setup.

The problem is, all my entities will do this, is there a way to state this without have to add this line for every entity? If I put .ForTypesThatDeriveFrom<Entity>(p => p.IgnoreProperty(x => x.IsDirty)), then I get an error trying to convert Entity to Address.

I also have entity set as the base type.

Thanks in advance, JT

解决方案

You create a class derived from DefaultAutomappingConfiguration and override the ShouldMap(Member member) method.

Like so:

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);
    }
}

and then your fluent configuration will look something like:

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

这篇关于FluentNHibernate - 自动映射忽略属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆