代码之家  ›  专栏  ›  技术社区  ›  Carl Hörberg

如何在nhibernate中按属性忽略属性

  •  1
  • Carl Hörberg  · 技术社区  · 16 年前

    AttributePropertyConvention 似乎没有这种能力,或者可以吗?找不到任何可以缝合的东西 IPropertyInstance 设置。。

    3 回复  |  直到 16 年前
        1
  •  3
  •   Ryan O    14 年前

    我试着用这两个建议中的任何一个来创建一个约定,甚至两个都没有,似乎都不适用于流利的nhibernate 1.3.0.727

    public class IgnoreAttributeConvention : AttributePropertyConvention<IgnoreAttribute>
    {
        protected override void Apply(IgnoreAttribute attribute, IPropertyInstance instance)
        {
            instance.ReadOnly();
        }
    }
    
    
    public class IgnoreAttributeConvention : AttributePropertyConvention<IgnoreAttribute>
    {
        protected override void Apply(IgnoreAttribute attribute, IPropertyInstance instance)
        {
            instance.Access.None();
        }
    }
    
    public class IgnoreAttributeConvention : AttributePropertyConvention<IgnoreAttribute>
    {
        protected override void Apply(IgnoreAttribute attribute, IPropertyInstance instance)
        {
            instance.Access.None();
            instance.ReadOnly();
        }
    }
    

    我后来发现了这个google groups讨论,虽然以前的讨论指出不能使用约定忽略属性,但如果使用自动映射,则必须重写类映射。

    https://groups.google.com/forum/?fromgroups#!topic/fluent-nhibernate/PDOBNzdJcc4

        2
  •  0
  •   Carl Hörberg    16 年前

    这很容易:

    public class IgnoreAttributeConvention : AttributePropertyConvention<IgnoreAttribute>
    {
        protected override void Apply(IgnoreAttribute attribute, IPropertyInstance instance)
        {
            instance.ReadOnly();
        }
    }
    

        3
  •  0
  •   John Holliday    14 年前

    ReadOnly()方法告诉FNH不要在数据库中查找属性的更改。要完全忽略该属性,需要调用instance.Access.None()。

        4
  •  0
  •   keithyip    7 年前

    以下代码将阻止在数据库中生成列。

    public class MyEntity
    {
        [NotMapped]
        public bool A => true;
    }
    
    public class AutomappingConfiguration : DefaultAutomappingConfiguration
    {
        public override bool ShouldMap(Member member)
        {
            if (member.MemberInfo.GetCustomAttributes(typeof(NotMappedAttribute), true).Length > 0)
            {
                return false;
            }
            return base.ShouldMap(member);
        }
    }
    
    推荐文章