This took me some time to find and work out so I am posting this in case I ever need again. I was getting mapping errors so wanted to see what Fluent NH spits out.
public class FluentNHUtil
{
string schemaExportPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"FluentNHMappings");
[Observation]
public void compile_mappings_to_disk()
{
if (Directory.Exists(schemaExportPath))
Directory.Delete(schemaExportPath, true);
Directory.CreateDirectory(schemaExportPath);
var model = new ReportingModelPersistenceMappingHandler().CreateModel();
model.CompileMappings();
model.WriteMappingsTo(schemaExportPath);
}
}
public class ReportingModelPersistenceMappingHandler
{
public MappingConfiguration Handle(MappingConfiguration mappingConfiguration)
{
mappingConfiguration.AutoMappings.Add(CreateModel());
mappingConfiguration.HbmMappings.AddFromAssembly(GetType().Assembly);
return mappingConfiguration;
}
public AutoPersistenceModel CreateModel()
{
//the Components convention seems redundant but is dealing with an oddity in FluentNH
return AutoMap.Assembly(GetType().Assembly)
.Setup(s =>
{
s.IsComponentType = type => type.Namespace.Contains("Components");
})
.Conventions.Add(
new ReportingModelIdConvention(),
DefaultLazy.Never(),
new PluralizeTableNameConvention(),
new HasManyDefaultConvention(),
new LabNumberComponentConvention()
).Where(t => t.Namespace.Contains(GetNamespaceConvention()) && t.Namespace.Contains("Components")==false);
}
private string GetNamespaceConvention()
{
return GetType().Namespace + ".Model";
}
}
Posted
10-29-2009 4:03 PM
by
Michael Nichols