NHibernate: How to set DynamicUpdate on all Entiti

2019-08-03 22:28发布

How can I set DynamicUpdate and DynamicInsert on all my entities?

It works perfectly when I put it together with my mapping code, but I would like to specify it only once for my entire project.

I could not find an option when creating the Session or in the Configuration.

Any ideas?

2条回答
叛逆
2楼-- · 2019-08-03 23:09

I use fluent nhibernate so I would change them like this:

var fluentConfiguration = Fluently.Configure(NHibernate.Cfg.Configuration().Configure())
      .Mappings(m =>
          m.FluentMappings
          .AddFromAssemblyOf<OrderMap>()
          .Conventions.AddFromAssemblyOf<PascalCaseColumnNameConvention>())
          .ProxyFactoryFactory("NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate");

var config = fluentConfiguration.BuildConfiguration();

foreach(PersistentClass persistentClass in config.ClassMappings)
{
    persistentClass.DynamicUpdate = true;
}

var sessionFactory = config.BuildSessionFactory();
查看更多
手持菜刀,她持情操
3楼-- · 2019-08-03 23:20

When using mapping-by-code its as simple as this:-

var mapper = new ModelMapper();

mapper.BeforeMapClass += (mi, t, map) =>
    {
        map.DynamicUpdate(true);
        map.DynamicInsert(true);
    };

...

Just make sure you remove it from ALL your class by class mappings as they will override this convention.

Just to complete if you do want to override any class definitions then you can also use

mapper.AfterMapClass += (mi, t, map) => { ... }
查看更多
登录 后发表回答