static void CustomizeEnumPrimaryKey( System.Type type, NHibernate.Mapping.ByCode.IClassAttributesMapper classAttributesMapper ) { var primaryKeyProperty = type.GetMember( "Enum", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance ); if (primaryKeyProperty.Length == 1) { System.Reflection.MemberInfo mi = primaryKeyProperty[0]; classAttributesMapper.Id(mi, idMapper => { idMapper.Column("enum"); idMapper.Generator(NHibernate.Mapping.ByCode.Generators.Assigned); }); } }
static void mapper_BeforeMapClass(NHibernate.Mapping.ByCode.IModelInspector modelInspector, System.Type type, NHibernate.Mapping.ByCode.IClassAttributesMapper classCustomizer) { classCustomizer.Cache(cacheMapping => cacheMapping.Usage(NHibernate.Mapping.ByCode.CacheUsage.ReadWrite)); string fullName = type.FullName; // example: Domain.TheProduction+Product string[] fullNameSplit = fullName.Split('+'); string className = fullNameSplit[1]; // Last() skips the other namespace(s) string schemaDomainName = fullNameSplit[0].Split('.').Last(); string schemaName = schemaDomainName.Substring(0, schemaDomainName.Length - "Domain".Length); string sqlServerFullName = schemaName + "." + className; classCustomizer.Table(sqlServerFullName); System.Reflection.MemberInfo mi = type.GetMember(className + "Id", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)[0]; classCustomizer.Id(mi, idMapper => { idMapper.Column(className + "Id"); idMapper.Generator(NHibernate.Mapping.ByCode.Generators.Identity); }); }
static void CustomizeIdPrimaryKey( System.Type type, NHibernate.Mapping.ByCode.IClassAttributesMapper classAttributesMapper, string schemaName, string tableName ) { var primaryKeyProperty = type.GetMember( "Id", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance ); if (primaryKeyProperty.Length == 1) { System.Reflection.MemberInfo mi = primaryKeyProperty[0]; var propertyType = (System.Reflection.PropertyInfo)mi; classAttributesMapper.Id(mi, idMapper => { idMapper.Column("id"); if (propertyType.PropertyType == typeof(int)) { idMapper.Generator( NHibernate.Mapping.ByCode.Generators.Sequence, generatorMapping => generatorMapping.Params(new { sequence = schemaName + "." + tableName + "_id_seq" }) ); } else if (propertyType.PropertyType == typeof(System.Guid)) { idMapper.Generator(NHibernate.Mapping.ByCode.Generators.Assigned); } else { throw new System.ArgumentException( string.Format("Id with type of {0} is unusual. Class: {1}", propertyType.PropertyType, type) ); } }); } }