Hello Friends, I am new to NHibernate. We have a requirement to add new properties to the dynamic component at run-time. We are using and IDictionary in our class which we map to an dynamic component in the Nhibernate map. Here is the class definition and map, we are using (slightly modified).
public class Parent { public virtual long Id { get; set; } public virtual IDictionary ExtensionData { get; set; }
public Parent() { ExtensionData = new Dictionary<string, object>(); } }
<class name="Parent" table="Parent"> <id name="Id" column="Id" type="long"> <generator class="native" /> </id> <dynamic-component name="ExtensionData" > <property name="m_DateTime" type="DateTime" /> </dynamic-component> </class>
As you will find that we already have a property "m_DateTime" of DateTime type in our Dictionary, and now we want to add "m_string" of String type at runtime into dynamic component. Here is the code we have for it.
NHibernate.Mapping.PersistentClass _test1 = _cfg.GetClassMapping(typeof(Parent)); NHibernate.Mapping.Property _newproperty = new NHibernate.Mapping.Property(); NHibernate.Mapping.SimpleValue _simpleValue = new NHibernate.Mapping.SimpleValue(_test1.Table); NHibernate.Mapping.Column _newcolumn = new NHibernate.Mapping.Column("m_string"); _simpleValue.AddColumn(_newcolumn); _simpleValue.TypeName = "string"; _newproperty.Name = "m_string"; _newproperty.Value = _simpleValue; NHibernate.Mapping.Property _ExtensionData = _test1.GetProperty("ExtensionData"); NHibernate.Mapping.Component _compExtensionData = (NHibernate.Mapping.Component)_ExtensionData.Value; _compExtensionData.AddProperty(_newproperty); new SchemaUpdate(_cfg).Execute(true, true); _cfg.BuildSessionFactory();
I am getting an exception "property mapping has wrong number of columns: " when trying to build the session factory. I appreciate any help.
Thanks in advance
|