Fluent NHibernate使用FluentMappings忽略ClassMap中的属性

我在我的项目中使用NHibernate 3.1和Fluent NHibernate作为ORM。 我需要Fluent NHibernate忽略POCO的属性。 起初,我的post看起来可能与此问题完全相同,但事实并非如此。

我的并发症首先来自POCO定义在不同于映射的assembly中,并且我正在使用流畅的映射来进行POCO。 我还有其他要求,即不在会话工厂配置发生的情况下编写ingore-property代码(这发生在模块外部的集中位置),而是作为定义映射的模块的一部分。 理想情况下,我认为正确的位置将是具体的ClassMap实现,因为它确切地知道如何向ORM描述POCO。

但是,我坚持这个主要是因为这是我对NHibernate及其流畅API的第一次影响。 到目前为止,我对其function和可扩展性有非常好的印象,我希望有一种方法可以通过将映射相关代码封装在相应模块中的方式来实现我的需求。

这是我的配置,从一个集中的地方:

 List assemblies = GetModules().Select(x => x.GetType().Assembly).ToList(); ISessionFactory nhibernateSessionFactory = Fluently .Configure() .Mappings(m => assemblies.ForEach(asm => m.FluentMappings.AddFromAssembly(asm))) .Database( MsSqlConfiguration.MsSql2005 .ShowSql() .ConnectionString(DatabaseConfig.Instance.ConnectionString)) .ExposeConfiguration(c => new SchemaUpdate(c).Execute(true, true)) .BuildSessionFactory(); 

我使用从ClassMapinheritance的标准类映射:

 public class User { public virtual int ID { get; set; } public virtual String Username { get; set; } public virtual String Password { get; set; } public virtual DateTime DateCreated { get; set; } public virtual DateTime DateModified { get; set; } // Must ignore public string ComputedProperty { get { ... } } } public class UserMap : ClassMap { public UserMap() { Table("User"); Id(x => x.ID).GeneratedBy.Identity(); Map(m => m.Username).Not.Nullable().Length(255).UniqueKey("User_Username_Unique_Key"); Map(m => m.Password).Not.Nullable().Length(255); Map(m => m.DateCreated).Not.Nullable(); Map(m => m.DateModified).Not.Nullable(); } } 

我认为你是对的, ClassMap是忽略这个属性的最佳位置。

例:

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

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

至于从另一个程序集获取映射,它应该像这样简单(取决于您当前的配置):

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

我知道这篇文章有点老了,但我还是发帖,因为我没有找到关于这个主题的任何最新post。 我想最简单的方法应该是为每个我们不希望持久保存到表的属性添加属性。 通过添加一个扩展,检查它是否具有例如。 有一个[NoEntity] attibute。

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

并在流畅的配置设置:

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

不会贾斯汀。 这是扩展的事情。 只是您想要的属性被忽略。

 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 }