Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping a struct as Id in nhibernate
    primarykey
    data
    text
    <p>I have the following struct and class:</p> <pre><code>public struct DepartmentId{ public int Value {get; set;} } public class Department{ public virtual DepartmentId Id{get;set;} public virtual string Name {get; set;} } </code></pre> <p>I created a mapping file for Department as follows:</p> <pre><code>public class DepartmentMapping : ClassMapping&lt;Department&gt;{ public DepartmentMapping{ Table("Department"); Id(dept =&gt; dept.Id, mapper =&gt; { mapper.Column("Id"); mapper.Type(new DepartmentIdType()); }); Property(dept =&gt; dept.Name, mapper =&gt; mapper.Column("Name")); } } </code></pre> <p>where DepartmentIdType implements IIdentifierType:</p> <pre><code>class DepartmentIdType : PrimitiveType, IIdentifierType { public DepartmentIdType() : base(SqlTypeFactory.Int32) { } public override object DeepCopy(object val, EntityMode entityMode, ISessionFactoryImplementor factory) { return val; } public override object Replace(object original, object current, ISessionImplementor session, object owner, IDictionary copiedAlready) { return original; } public override Type ReturnedClass { get { return typeof(DepartmentId); } } public object StringToObject(string xml) { return new DepartmentId {Value = int.Parse(xml)}; } public override string Name { get { return typeof(DepartmentId).Name; } } public override void Set(IDbCommand cmd, object value, int index) { var id = (DepartmentId) value; ((IDataParameter) cmd.Parameters[index]).Value = id.Value; } public override object Get(IDataReader rs, int index) { int value = rs.GetInt32(index); return new DepartmentId {Value = value}; } public override object Get(IDataReader rs, string name) { return Get(rs, rs.GetOrdinal(name)); } public override string ToString(object val) { if (val == null) return ""; return val.ToString(); } public override object FromStringValue(string xml) { return new DepartmentId {Value = Int32.Parse(xml)}; } public override object DefaultValue { get { return new DepartmentId {Value = 0}; } } public override string ObjectToSQLString(object value, NHibernate.Dialect.Dialect dialect) { return value.ToString(); } public override Type PrimitiveClass { get { return typeof(DepartmentId); } } } </code></pre> <p>However, at the time of creating the HbmMapping, I get the following error:</p> <pre><code>Could not compile the mapping document: mapping_by_code NHibernate.MappingException: Could not compile the mapping document: mapping_by_code ---&gt; NHibernate.MappingException: Could not determine type for: Demo.Models.DepartmentId, Demo.Models, for columns: NHibernate.Mapping.Column(Id) at NHibernate.Mapping.SimpleValue.get_Type() at NHibernate.Cfg.XmlHbmBinding.ClassIdBinder.CreateIdentifierProperty(HbmId idSchema, PersistentClass rootClass, SimpleValue id) at NHibernate.Cfg.XmlHbmBinding.ClassIdBinder.BindId(HbmId idSchema, PersistentClass rootClass, Table table) at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(HbmClass classSchema, IDictionary`2 inheritedMetas) at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(HbmClass rootClass, IDictionary`2 inheritedMetas) at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary`2 inheritedMetas) at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema) at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) </code></pre> <p>How do I fix this issue (without changing DepartmentId from struct to class)?</p> <p>Thanks in advance for your help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload