I usually have no problem ‘seeding’ my database via NHibernate like this:
...
string[] mappingAssemblies = new string[] { "Bla.Di.Domain" };
string configFile = "NHibernate.config";
NHibernate.Cfg.Configuration config = NHibernateSession.Init(
new SimpleSessionStorage(),
mappingAssemblies,
new AutoPersistenceModelGenerator().Generate(),
configFile);
TextWriter writeFile = new StreamWriter("d:/SeedSQL.sql");
var session = NHibernateSession.GetDefaultSessionFactory().OpenSession();
new SchemaExport(config).Execute(true, true, false, session.Connection, writeFile);
For some reason the database is not created for the above dll (Bla.Di.Domain) with a simple class like this:
namespace Bla.Di.Domain
{
using SharpArch.Domain.DomainModel;
public class Test : Entity
{
public virtual string X { get; set; }
}
}
Is there anything that could have gone potentially go wrong? I do not get an exception. I referenced the dll in my ‘seeding’ project. Maybe there is an issue with the file location of my dll (it is a fairly complicated solution). Thanks.
PS (as requested in comment):
This is my AutoPersistenceModelGenerator – please note that it lives in another namespace:
public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
{
public AutoPersistenceModel Generate()
{
//var mappings = AutoMap.AssemblyOf<Bla>(new AutomappingConfiguration());
var mappings = AutoMap.AssemblyOf<Test>(new AutomappingConfiguration());
mappings.IgnoreBase<Entity>();
mappings.IgnoreBase(typeof(EntityWithTypedId<>));
mappings.Conventions.Setup(GetConventions());
mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
return mappings;
}
private static Action<IConventionFinder> GetConventions()
{
return c =>
{
c.Add<PrimaryKeyConvention>();
c.Add<CustomForeignKeyConvention>();
c.Add<HasManyConvention>();
c.Add<TableNameConvention>();
};
}
}
I have added a reference to the domain model (different name space) and changed:
var mappings = AutoMap.AssemblyOf<Bla>(new AutomappingConfiguration());
to:
var mappings = AutoMap.AssemblyOf<Test>(new AutomappingConfiguration());
Let us say we have two namespaces: CompanyName.X and CompanyName.Y.
CompanyName.X contains class A and CompanyName.Y contains B, where A and B inherit from Entity. Then this adaptation helps:
Original:
Adapted code: