Fluent NHibernate ignore property inside the ClassMap, using FluentMappings

风流意气都作罢 提交于 2019-12-05 18:31:04

I think you are right that the ClassMap is the best place to ignore this property.

Example:

.Override<Shelf>(map =>  
{  
  map.IgnoreProperty(x => x.YourProperty);
});

Documentation: https://github.com/jagregory/fluent-nhibernate/wiki/Auto-mapping#ignoring-properties

As far as getting the mappings from another assembly, it should be as easy as something like this (depending on your current configuration):

.Mappings(m =>
              {
                  m.FluentMappings.AddFromAssemblyOf<ProvideClassFromYourOtherAssembly>();
              });

I know this post is bit old, but I post anyway since I didn't find any up todate posts on the subject. I guess the easiest way should be to add an attribute to each property we dont want to be persisted to a table. By add a extension that check if it has for eg. has a [NoEntity] attibute.

/// <summary>
/// Tells a single Property to not be persisted to table.
/// </summary>
public class NoEntity : Attribute { }


    /// <summary>
/// Extension to ignore attributes
/// </summary>
public static class FluentIgnore
{
    /// <summary>
    /// Ignore a single property.
    /// Property marked with this attributes will no be persisted to table.
    /// </summary>
    /// <param name="p">IPropertyIgnorer</param>
    /// <param name="propertyType">The type to ignore.</param>
    /// <returns>The property to ignore.</returns>
    public static IPropertyIgnorer SkipProperty(this IPropertyIgnorer p, Type propertyType)
    {
        return p.IgnoreProperties(x => x.MemberInfo.GetCustomAttributes(propertyType, false).Length > 0);
    }
}

And in the fluent config setup:

            return Fluently.Configure()
            .Database(DatabaseConfig)
            .Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(typeof(IDependency).Assembly)
            .OverrideAll(p => {
                p.SkipProperty(typeof(NoEntity)); 
            }).Where(IsEntity)))
            .ExposeConfiguration(ValidateSchema)
            .ExposeConfiguration(BuildSchema)
            .BuildConfiguration();

Will not Justin. This is the thing with this extension. Just the property you want gets ignored.

public class Person : IEntity{ 
 public virtual string Name{..}
 public virtual string Lastname{..}

 [NoProperty]
 public virtual string FullName{ // Not created property
  get { return Name +  " " + Lastname;  }
 }
}

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