我如何创建一个流畅的NHibernate的公约忽略没有setter的属性 [英] How can I create a Fluent NHibernate Convention that ignores properties that don't have setters

查看:149
本文介绍了我如何创建一个流畅的NHibernate的公约忽略没有setter的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个 FluentNH (Fluent NHibernate)约定或配置,忽略所有属性没有设置:

它仍然会映射这些:

  public class foo {
public virtual int bar {get;私人设置;}
}

省略这些:

  public class foo {
public virtual int fizz {get; private set;}
public virtual int bar {get {return fizz;} } //< -------
}


解决方案你应该使用自定义的映射配置

pre $ public $ DefaultMappingConfiguration DefaultAutomappingConfiguration
{
public override bool应用地图(会员)
{
return member.CanWrite;


$ / code $ / pre

用法:

  var nhConfiguration = new Configuration()。Configure(); 
var mappingConfiguration = new DefaultMappingConfiguration();

var.fluentConfiguration =流利.Configure(nhConfiguration);
.Mappings(m => m.AutoMappings.Add(
AutoMap.AssemblyOf< MappedType>(mappingConfiguration)
));

var sessionFactory = this.fluentConfiguration.BuildSessionFactory();

但是,私人设置者不会被映射。你应该把他们作为保护


I'm looking for a FluentNH (Fluent NHibernate) convention or configuration that ignores all properties that have no setter:

It would still map these:

public class foo{
  public virtual int bar {get; private set;}
}

And omit these:

public class foo{
  public virtual int fizz{get;private set;}
  public virtual int bar{get {return fizz;}} //<-------
}

解决方案

You should use a custom mapping configuration

public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        return member.CanWrite;
    }
}

Usage :

var nhConfiguration = new Configuration().Configure();
var mappingConfiguration = new DefaultMappingConfiguration();

var.fluentConfiguration = Fluently.Configure(nhConfiguration );
    .Mappings(m => m.AutoMappings.Add(
        AutoMap.AssemblyOf<MappedType>(mappingConfiguration)
    ));

var sessionFactory = this.fluentConfiguration.BuildSessionFactory();

However, private setters won't get mapped. You should get them as protected

这篇关于我如何创建一个流畅的NHibernate的公约忽略没有setter的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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