FluentNHibernate - Automapping ignore property

和自甴很熟 提交于 2019-12-03 20:36:55
ungood

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

I've had the same problem, except that NHibernate complained about a missing class.

I believe you can use the following convention to remove the part that maps the property from the class:

public class IgnorePropertiesClassMapConvention : IClassConvention
{
    public bool Accept(IClassMap classMaps)
    {
        return true;
    }
    public void Apply(IClassMap classMap)
    {
        var partsToIgnore = classMap.Parts.OfType<IProperty>()
            .Where(IgnoreProperty).ToList();

        foreach (var part in partsToIgnore)
            ((IList<IMappingPart>)classMap.Parts).Remove(part);

    }

    private bool IgnoreProperty(IProperty property)
    {
        // Your logic would be here
    }
}

The code I've used is a bit different, since what I needed to remove was a ManyToOne mapping:

public class IgnorePartsBasedOnReturnTypeClassMapConvention : IClassConvention
{
    public bool Accept(IClassMap classMaps)
    {
        return true;
    }
    public void Apply(IClassMap classMap)
    {
        var partsToIgnore = classMap.Parts.OfType<IManyToOnePart>()
            .Where(IgnorePart).ToList();

        foreach (var part in partsToIgnore)
            ((IList<IMappingPart>)classMap.Parts).Remove(part);

    }
    private bool IgnorePart(IManyToOnePart part)
    {
        return IgnoreProperty(part.Property);
    }

    private bool IgnoreProperty(PropertyInfo propertyInfo)
    {
        var ignoredNamespaces = new []{"namespacesToIgnore"};
        return ignoredNamespaces.Any(namespc => propertyInfo.GetGetMethod().ReturnType.Namespace.StartsWith(namespc));
    }
}

I'm not exactly sure if this is the best way to do this. My feeling is that the discoverability part should be overriden with the ability to not even build up the part for unwanted properties, but for now this seems to work.

OverrideAll seems to fit well if you do not define your configuration.

Fluently
    .Configure()
    .Mappings(x => x.AutoMappings.Add(AutoMap.AssemblyOf<MyEntity>(mapConfig)
            .OverrideAll(ign => ign.IgnoreProperty("MyPropertyToBeIgnored"))
                ))

Source: https://bernardorezende.wordpress.com/2011/12/12/ignore-property-mapping-with-fluent-nhibernates-automapping-model/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!