NHibernate: How to set DynamicUpdate on all Entiti

2019-08-03 23:19发布

问题:

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?

回答1:

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


回答2:

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) => { ... }