I'm writing a small class that reads assemblies and generates tables for them.
Why? Because my main configuration file passed to hbm2ddl would regenerate all tables, and I want it preferably on a per-mapped-class granularity.
Anyhow, this is part of what I've got:
Code:
/// <summary>
/// Checks whether the tables in the <see cref="NHibernate.Cfg.Configuration"/> file exist.
/// </summary>
/// <param name="config">The configuration file to test.</param>
/// <returns>Whether the tables corresponding to the passed configuration file exist.</returns>
private static bool doesTableExist(Configuration config)
{
ISessionFactory factory = config.BuildSessionFactory();
using (ISession session = factory.OpenSession())
{
// Let's just try to find one object with the session and be happy.
ICriteria crit;
// I could use GetClassMetaData here instead, create a new config
// and then generate that table if no results were found, to make it
// MUCH more granular. But one step at a time.
IDictionary classMetaData = factory.GetAllClassMetadata();
foreach (object key in classMetaData.Keys)
{
crit = session.CreateCriteria(classMetaData [key].ClassType);
// So if the listing doesn't contain any results,
// tables don't exist, or it just doesn't matter if I redo the table.
if (crit.List().Count == 0)
return false;
}
}
return true;
}
I need help with the first part of the foreach loop. Obviously it's wrong, but I can't find documentation on what the IDictionary that's returned contains, so I don't know what to do with it.
Could someone help me with this, or perhaps should me a more effective way of doing the same thing?