簡體   English   中英

如何在NHibernate中按屬性忽略屬性

[英]how to ignore a property by attribute in nhibernate

如何通過用屬性裝飾屬性來忽略屬性? 基類AttributePropertyConvention似乎不具備該功能,或者可以嗎? IPropertyInstance上找不到可設置的任何內容。

我試圖創建一個包含兩個建議中的任何一個,甚至兩個建議的約定,似乎都與流暢的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網上論壇討論,盡管較早的狀態不能用約定忽略屬性,但是如果使用自動映射,則必須通過覆蓋類映射來完成。

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

那是舊的,我不知道它是否仍然有用,但這是我的經驗。 我希望這可以避免其他人嘗試使用此解決方案的麻煩,也可以促使其他人指出我可能會出問題的地方。

這很簡單:

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

其中IgnoreAttribute是簡單/空屬性。

以下代碼將防止在數據庫中生成列。

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

instance.ReadOnly()方法告訴FNH不要在數據庫中的屬性上查找更改。 要完全忽略該屬性,您需要調用instance.Access.None()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM