Fluent nhibernate Fluent NHibernate ShouldMap未检测到我的自定义属性

Fluent nhibernate Fluent NHibernate ShouldMap未检测到我的自定义属性,fluent-nhibernate,properties,automapping,skip,Fluent Nhibernate,Properties,Automapping,Skip,我已经花了几天时间来了解Fluent NHibernate自动映射工作模型。这很好,但我一直在检测模式中缺少的新细节。现在我想向类添加额外的属性,但不想将它们映射到数据库。典型的情况是,我需要具有内部逻辑的额外属性。 因此,我阅读了示例并扫描了StackOverflow,发现这不是要添加的另一个约定,而是继承DefaultAutomappingConfiguration并重写ShouldMap方法的问题 好的,没问题,一分钟后我有了这样的想法: public class CustomAutoma

我已经花了几天时间来了解Fluent NHibernate自动映射工作模型。这很好,但我一直在检测模式中缺少的新细节。现在我想向类添加额外的属性,但不想将它们映射到数据库。典型的情况是,我需要具有内部逻辑的额外属性。
因此,我阅读了示例并扫描了StackOverflow,发现这不是要添加的另一个约定,而是继承DefaultAutomappingConfiguration并重写ShouldMap方法的问题
好的,没问题,一分钟后我有了这样的想法:

public class CustomAutomappingConfiguration : DefaultAutomappingConfiguration
{

    public override bool ShouldMap(Member member)
    {
        var explicitSkip = member.PropertyType.GetCustomAttributes(typeof(SkipMap), false).Length > 0;
        if ((member.IsProperty && !member.CanWrite) || explicitSkip)
        {
            return false;
        }
        return base.ShouldMap(member);
    }
}


/// <summary>
/// Don't map this property to database.
/// </summary>
public class SkipMap : Attribute
{
}


public class DemoClass
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual MyBitwiseEnum Status { get; set; }

    public virtual bool IsValid
    {
        get
        {
            return (int)Status > 3;
        }
    }

    [SkipMap]
    public virtual bool IsBad
    {
        get
        {
            return MyBitwiseEnum.HasFlag(MyBitwiseEnum.Bad);
        }
        set
        {
            MyEnum = value ? MyBitwiseEnum | MyBitwiseEnum.Bad : MyBitwiseEnum ^ MyBitwiseEnum.Bad;
        }
    }
}
公共类CustomAutomappingConfiguration:DefaultAutomappingConfiguration
{
公共覆盖布尔ShouldMap(成员)
{
var explicitSkip=member.PropertyType.GetCustomAttributes(typeof(SkipMap),false)。长度>0;
if((member.IsProperty&&!member.CanWrite)| | explicitSkip)
{
返回false;
}
返回基地。ShouldMap(成员);
}
}
/// 
///不要将此属性映射到数据库。
/// 
公共类SkipMap:Attribute
{
}
公开课
{
公共虚拟整数Id{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟MyBitwiseEnum状态{get;set;}
公共虚拟bool是有效的
{
得到
{
返回(int)状态>3;
}
}
[跳过图]
公共虚拟bool是坏的
{
得到
{
返回MyBitwiseEnum.HasFlag(MyBitwiseEnum.Bad);
}
设置
{
MyEnum=value?MyBitwiseEnum | MyBitwiseEnum.Bad:MyBitwiseEnum^MyBitwiseEnum.Bad;
}
}
}
我知道我的演示课有点愚蠢,但它可以说明我的观点。
我的想法是手动决定要映射到数据库的属性。

readonly属性工作正常,因为ShouldMap方法将查找property.CanWrite。但肯定设置的自定义属性将不会被检测到。为什么

在常规方法中,我经常使用相同的方法,并且效果很好。为什么属性无法检测此处定义的属性,而在约定设置中它显然可以检测。有解决方法吗?

为了确保自定义属性适用于属性,请尝试将
[AttributeUsage(AttributeTargets.Property)]
添加到
SkipMap
类中


另一种可能性是属性名称与应用于不同目标的另一个属性冲突。尝试将类重命名为类似于
MyVerySpecialSkipMap
的名称,然后重新测试以验证没有属性冲突。至少,编写一些简单的反射代码,在应用程序的上下文之外测试
SkipMap
属性,以确保可以找到它。

您是否已将新的AutompConvention添加到Automap

AutoMap.AssemblyOf<>(new CustomAutomappingConfiguration())
应该是

member.MemberInfo.GetCustomAttributes(typeof(SkipMap), false)

谢谢你的回答!我添加了属性用法声明,没有引起任何抱怨。我按照建议更改了名称,但没有任何区别。具有此属性的属性仍映射到存储。这可能是FluentNHibernate中的一个bug吗?我应该在某个地方报告它吗?我会尝试通过创建一个简单的项目来排除故障,该项目只包含SkipMap类,反射代码将测试该类以查找属性。如果它在简单项目中不起作用,那么问题可能在Fluent NHibernate中。是的!我想你指的是自动配置。我也有几个约定,但它们都很好,不应该影响这个问题。谢谢!我真的错过了你的剪辑。但是今天我的一位同事发现了同样的事情,所以我只是到这里来用这个简单的调整来更新这个职位。MemberInfo而不是PropertyType起了作用!
member.MemberInfo.GetCustomAttributes(typeof(SkipMap), false)