Nhibernate:数据库中可能不存在的Map列

时间:2014-03-05 16:41:52

标签: nhibernate nhibernate-mapping

有没有办法映射数据库中可能存在或可能不存在的列,还是将其动态映射到列?

1 个答案:

答案 0 :(得分:3)

您可以动态更改映射,但我认为没有办法映射可能存在或可能不存在的列。下面的例子是你如何使用流利的nhibernate。

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)
{
    //Check to see if there are any missing columns from this mapping.
    //If so remove the column from the mapping.

    //TODO: Query the database and catch errors related to missing columns
    //      If a column is missing remove it
}

var sessionFactory = config.BuildSessionFactory();