有没有办法映射数据库中可能存在或可能不存在的列,还是将其动态映射到列?
答案 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();