Actually, I haven't released any version of TinyERP in Codeplex. In the past, I created a sample database (not generate DB automatically from schema). Anyway, I need to check this issue.
After several minutes checking, I found the issue:
In TinyERP, I use NHibernate attribute on Business Entity for mapping. To use this, we have to override a LocalSessionFactoryObject to add assembly contains Business Entity classes into HbmSerializer. HbmSerializer only helps us to create Hibernate mapping config in stream. A very stupid thing is: the code to check and generate DB schema does not exist in parent classes of LocalSessionFactoryObject => Nothing happens when you set hibernate.hbm2ddl.auto="create".
The old code
public class LocalSessionFactoryObjectImp : LocalSessionFactoryObject { protected override void PostProcessConfiguration(NHibernate.Cfg.Configuration config) { base.PostProcessConfiguration(config); HbmSerializer.Default.Validate = true; IListmodules = ApplicationConfiguration.GetInstance().Modules; foreach (Module module in modules) { Assembly assemblyInAppDomain = FindAssembly(module.Assembly); if (assemblyInAppDomain != null) { config.AddInputStream(HbmSerializer.Default.Serialize(assemblyInAppDomain)); } } }
The above code is a common code template to override LocalSessionFactoryObject - that we mostly see in many forums. The problem is: default SessionFactoryObject in NHibernate keeps the code to export DB schema. However, LocalSessionFactoryObject and its parent class do not implement any line of code for this. So, we have to add this feature manually if we want to override it.
This is the new code to fix this issue:
public class LocalSessionFactoryObjectImp : LocalSessionFactoryObject { protected override void PostProcessConfiguration(NHibernate.Cfg.Configuration config) { base.PostProcessConfiguration(config); HbmSerializer.Default.Validate = true; IListmodules = ApplicationConfiguration.GetInstance().Modules; foreach (Module module in modules) { Assembly assemblyInAppDomain = FindAssembly(module.Assembly); if (assemblyInAppDomain != null) { config.AddInputStream(HbmSerializer.Default.Serialize(assemblyInAppDomain)); } } SchemaExport schemaExport = new SchemaExport(config); string autoCreateDBMode = config.GetProperty("hibernate.hbm2ddl.auto"); if (!string.IsNullOrEmpty(autoCreateDBMode) && autoCreateDBMode == "create") { schemaExport.Create(true, true); } } }
Hope it's useful in case you have same problem with using NHiberate attribute for mapping and auto creating DB schema.
No comments:
Post a Comment