功能NHibernate忽略属性ClassMap内,使用FluentMappings [英] Fluent NHibernate ignore property inside the ClassMap, using FluentMappings

查看:430
本文介绍了功能NHibernate忽略属性ClassMap内,使用FluentMappings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NHibernate 3.1和功能NHibernate在我的项目ORM。我需要用流利的NHibernate忽略POCO的属性。起初,我的帖子可能如下的这个问题时,但是事实并非如此。



我的并发症来首次从该波苏斯在不同的装配比映射定义的事实,我现在用的流利的映射作为我的波苏斯。我有额外的要求,不写则会忽略属性代码,其中会话工厂配置发生(这发生在模块外集中的地方),但作为定义映射模块的一部分。理想情况下,我认为合适的地方将是具体 ClassMap 的实施,因为它知道究竟该如何描述一个POCO的ORM。



不过,我被困在这主要是因为这是我与NHibernate和流利的API第一次冲击。到现在为止我有它的功能和可扩展性非常美好的印象,我希望有一种方式来实现我的方式,映射相关的代码封装及其相应模块中的要求。



下面是我的配置,从集中的地方:

 列表<大会> 。组件= GetModules()选择(X => x.GetType()汇编。).ToList(); 

ISessionFactory nhibernateSessionFactory =流利
.Configure()
.Mappings(M = GT; assemblies.ForEach(ASM => m.FluentMappings.AddFromAssembly(ASM)))
.Database(
MsSqlConfiguration.MsSql2005
.ShowSql()
.ConnectionString(DatabaseConfig.Instance.ConnectionString))
.ExposeConfiguration(C =>新建SchemaUpdate工具(C ).Execute(真,真))
.BuildSessionFactory();



我使用的标准类的映射,从 ClassMap :

 公共类用户
{
公共虚拟INT ID {搞定;组; }
公共虚拟用户名字符串{搞定;组; }
公共虚拟字符串密码{搞定;组; }
公共虚拟的DateTime dateCreated会获得{;组; }
公共虚拟的DateTime DateModified {搞定;组; }

//必须忽略
公共字符串ComputedProperty {获得{...}}
}

公共类中userMap:ClassMap<使用者>
{
公众用户映射()
{
表(用户);
ID(X => x.ID).GeneratedBy.Identity();
地图(M = GT; m.Username).Not.Nullable()长度(255).UniqueKey(User_Username_Unique_Key);
地图(M = GT; m.Password).Not.Nullable()长度(255)。
地图(M = GT; m.DateCreated).Not.Nullable();
地图(M = GT; m.DateModified).Not.Nullable();
}
}


解决方案

我。认为你是正确的 ClassMap 是忽略这个属性的最佳去处。



例如:

  .Override<保质>(地图=> 
{
map.IgnoreProperty(X => x.YourProperty );
});



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



至于正从另一个程序集的映射,它应该是为这样的事情一样方便(根据您当前的配置):

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


I am using NHibernate 3.1 and Fluent NHibernate as ORM in my project. I need to have a property of a POCO ignored by Fluent NHibernate. At first, my post might look as exact duplicate of this question, but it is not.

My complications come first from the fact that the POCOs are defined in a different assembly than the mapping and I am using fluent mappings for my POCOs. I have additional requirement not to write ingore-property code where the session factory configuration takes place (this happens at a centralized place outside the modules), but as part of the module that defines the mappings. Ideally, I believe the right place would be the concrete ClassMap implementation, since it knows exactly how to describe a POCO to the ORM.

However, I am stuck on this mainly because this is my first impact with NHibernate and its fluent API. Up to now I am having very good impression of its capabilities and extensibility, and I hope there is a way to achieve my requirement in a way that the mapping related code is encapsulated in its corresponding module.

Here is my configuration, from a centralized place:

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

I use standard class mappings that inherit from ClassMap:

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

解决方案

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

这篇关于功能NHibernate忽略属性ClassMap内,使用FluentMappings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆