public void Init() { MappingAssembly = GetType().Assembly; Mapper = new ConventionModelMapper(); Configuration = new global::NHibernate.Cfg.Configuration(); Mapper.IsPersistentId((m, declared) => declared || m.Name == "Id"); Mapper.BeforeMapBag += (inspector, member, customizer) => customizer.Key(k => k.Column(member.GetContainerEntity(inspector).Name + "Id")); Mapper.BeforeMapManyToOne += (inspector, member, customizer) => customizer.Column(member.LocalMember.Name + "Id"); Mapper.AfterMapProperty += (inspector, member, customizer) => { var propertyInfo = ((PropertyInfo)member.LocalMember); var propertyType = propertyInfo.PropertyType; if (typeof(ValueType).IsAssignableFrom(propertyType) && !propertyType.IsNullable()) { customizer.Column(c => c.Default(GetDefaultValue(propertyInfo))); customizer.NotNullable(true); } }; var simpleModelInspector = (SimpleModelInspector)Mapper.ModelInspector; simpleModelInspector.IsRootEntity((type, declared) => { var modelInspector = (IModelInspector)simpleModelInspector; return(declared || type.IsClass //если наследуемся от класса который не маплен то это простое наследование && (typeof(object) == type.BaseType || !modelInspector.IsEntity(type.BaseType)) && modelInspector.IsEntity(type)); }); var baseInspector = (IModelInspector) new SimpleModelInspector(); Mapper.IsEntity((type, declared) => { return(declared || baseInspector.IsEntity(type) || Configuration.ClassMappings.Any(m => m.MappedClass == type)); }); Configuration.AddProperties(new Dictionary <string, string> { { Environment.Dialect, "NHibernate.Dialect.MySQL5Dialect" }, { Environment.ConnectionDriver, "NHibernate.Driver.MySqlDataDriver" }, { Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider" }, { Environment.ConnectionStringName, "db" }, { Environment.Hbm2ddlKeyWords, "none" }, //раскомментировать если нужно отладить запросы хибера //{Environment.ShowSql, "true"}, //{Environment.FormatSql, "true"}, { Environment.Isolation, "ReadCommitted" }, }); Mapper.Class <Producer>(m => { m.Schema("catalogs"); }); Mapper.Class <Product>(m => { m.Id(s => s.Id); m.Schema("Catalogs"); m.Table("Products"); }); Mapper.Class <Catalog>(m => { m.Id(s => s.Id); m.Schema("Catalogs"); m.Table("Catalog"); m.ManyToOne(s => s.CatalogForm, mapper => mapper.Column("FormId")); m.ManyToOne(s => s.CatalogName, mapper => mapper.Column("NameId")); }); Mapper.Class <CatalogName>(m => { m.Id(s => s.Id); m.Schema("Catalogs"); m.Table("CatalogNames"); m.Bag(o => o.Catalogs, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(k => k.Column("NameId")); }); m.ManyToOne(s => s.Mnn, mapper => mapper.Column("MnnId")); }); Mapper.Class <CatalogForm>(m => { m.Id(s => s.Id); m.Schema("Catalogs"); m.Table("CatalogForms"); }); Mapper.Class <Mnn>(m => { m.Id(s => s.Id); m.Schema("Catalogs"); m.Table("Mnn"); m.Property(x => x.Name, c => c.Column("Mnn")); }); Mapper.Class <Promoter>(m => { m.Bag(o => o.Associations, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); }); }); Mapper.Class <Association>(m => { m.ManyToOne(s => s.Supplier, mapper => mapper.Column("SupplierId")); m.Bag(o => o.Members, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); }); m.Bag(o => o.MarketingEvents, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); }); m.Bag(o => o.Promoters, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); }); m.Bag(o => o.Regions, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); }); m.Bag(o => o.Contacts, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); }); }); Mapper.Class <MarketingEvent>(m => { m.Bag(o => o.Producers, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(k => k.Column("MarketingEventId")); }); m.Bag(o => o.Promotions, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(k => k.Column("MarketingEventId")); }); }); Mapper.Class <PromoterProducer>(m => { m.ManyToOne(s => s.Producer, mapper => mapper.Column("ProducerId")); m.ManyToOne(s => s.MarketingEvent, mapper => mapper.Column("MarketingEventId")); }); Mapper.Class <PromotionMember>(m => { m.Bag(o => o.Subscribes, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(k => k.Column("MemberId")); }); m.Bag(o => o.AddressLimits, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(k => k.Column("MemberId")); }); m.Bag(o => o.LegalEntityLimits, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(k => k.Column("MemberId")); }); }); Mapper.Class <AddressLimit>(m => { m.ManyToOne(s => s.Member, mapper => mapper.Column("MemberId")); m.ManyToOne(s => s.Address, mapper => mapper.Column("AddressId")); }); Mapper.Class <LegalEntityLimit>(m => { m.ManyToOne(s => s.Member, mapper => mapper.Column("MemberId")); m.ManyToOne(s => s.LegalEntity, mapper => mapper.Column("LegalEntityId")); }); Mapper.Class <PromotionProduct>(m => { m.ManyToOne(s => s.Product, mapper => mapper.ForeignKey("ProductId")); m.ManyToOne(s => s.Promotion, mapper => mapper.ForeignKey("PromotionId")); m.Bag(o => o.Replacements, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(k => k.Column("PromotionProductId")); }); }); Mapper.Class <PromotionReplacement>(m => { m.ManyToOne(s => s.Catalog, mapper => mapper.Column("CatalogId")); }); Mapper.Class <PromotionSupplier>(m => { m.ManyToOne(s => s.Supplier, mapper => mapper.ForeignKey("SupplierId")); m.ManyToOne(s => s.Promotion, mapper => mapper.ForeignKey("PromotionId")); }); Mapper.Class <ProducerPromotion>(m => { m.ManyToOne(s => s.MarketingEvent, mapper => mapper.Column("MarketingEventId")); m.Bag(o => o.Products, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(s => s.Column("PromotionId")); }, map => map.OneToMany(s => s.Class(typeof(PromotionProduct)))); m.Bag(o => o.Suppliers, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(s => s.Column("PromotionId")); }, map => map.OneToMany(s => s.Class(typeof(PromotionSupplier)))); m.Bag(o => o.Subscribes, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(s => s.Column("PromotionId")); }, map => map.OneToMany(s => s.Class(typeof(PromotionSubscribe)))); m.Bag(o => o.FeePercents, c => { c.Cascade(Cascade.All); c.Inverse(true); c.Key(s => s.Column("PromotionId")); }); m.Bag(o => o.FeeSums, c => { c.Cascade(Cascade.All); c.Inverse(true); c.Key(s => s.Column("PromotionId")); }); }); Mapper.Class <PromotionPercent>(m => { m.ManyToOne(s => s.Promotion, mapper => mapper.Column("PromotionId")); }); Mapper.Class <PromotionSum>(m => { m.ManyToOne(s => s.Promotion, mapper => mapper.Column("PromotionId")); }); Mapper.Class <PromotionSubscribe>(m => { m.ManyToOne(s => s.Member, mapper => mapper.Column("MemberId")); m.ManyToOne(s => s.Promotion, mapper => mapper.Column("PromotionId")); }); Mapper.Class <Address>(m => { m.Property(x => x.AddressName, c => c.Column("Address")); }); Mapper.Class <RegionalAdmin>(m => { m.Schema("AccessRight"); m.Table("RegionalAdmins"); m.Id(x => x.RowId); m.Property(x => x.Name, c => c.Column("UserName")); }); Mapper.Class <Region>(m => { m.Schema("Farm"); m.Table("Regions"); m.Id(x => x.Id, mapper => mapper.Column("RegionCode")); m.Property(x => x.Name, mapper => mapper.Column("Region")); }); Mapper.Class <Client>(m => { m.ManyToOne(c => c.Region, mapper => mapper.Column("RegionCode")); m.Bag(o => o.ContactGroups, c => { c.Cascade(Cascade.None); c.Inverse(true); c.Key(k => { k.PropertyRef(x => x.ContactGroupOwnerId); k.Column("ContactGroupOwnerId"); }); }); m.Bag(o => o.Payers, c => { c.Cascade(Cascade.None); c.Inverse(true); c.Key(s => s.Column("ClientId")); }); }); Mapper.Class <PayerClient>(m => { m.ManyToOne(c => c.Client, mapper => mapper.Column("ClientId")); m.ManyToOne(c => c.Payer, mapper => mapper.Column("PayerId")); }); Mapper.Class <Payer>(m => { m.Id(x => x.Id, mapper => mapper.Column("PayerID")); m.Schema("Billing"); m.Table("Payers"); m.Bag(o => o.LegalEntities, c => { c.Cascade(Cascade.None); c.Inverse(true); c.Key(s => s.Column("PayerId")); }); }); Mapper.Class <LegalEntity>(m => { m.Schema("Billing"); m.Table("LegalEntities"); m.ManyToOne(c => c.Payer, mapper => mapper.Column("PayerId")); }); Mapper.Class <Contact>(m => { m.Schema("Contacts"); m.Table("Contacts"); m.Property(x => x.ContactType, mapper => mapper.Column("Type")); }); Mapper.Class <ContactGroup>(m => { m.Id(x => x.Id, mapper => mapper.Generator(new AssignedGeneratorDef())); m.Schema("Contacts"); m.Table("Contact_groups"); m.Property(x => x.ContactGroupTypeId, mapper => mapper.Column("Type")); m.Bag(o => o.Contacts, c => { c.Cascade(Cascade.All | Cascade.DeleteOrphans); c.Inverse(true); c.Key(k => { k.PropertyRef(x => x.Id); k.Column("ContactOwnerId"); }); }); }); Mapper.Class <ContactOwner>(m => { m.Schema("Contacts"); m.Table("Contact_owners"); }); Mapper.Class <ContactGroupOwner>(m => { m.Schema("Contacts"); m.Table("Contact_group_owners"); }); var types = MappingAssembly.GetTypes().Where(t => !Attribute.IsDefined(t, typeof(CompilerGeneratedAttribute), false) && Mapper.ModelInspector.IsRootEntity(t)); var mapping = Mapper.CompileMappingFor(types.Distinct()); var @class = Generators.Native.Class; foreach (var rootClass in mapping.RootClasses.Where(c => c.Id != null)) { if (rootClass.Id.generator == null) { rootClass.Id.generator = new HbmGenerator { @class = @class }; } } Configuration.SetNamingStrategy(new PluralizeNamingStrategy()); Configuration.AddDeserializedMapping(mapping, MappingAssembly.GetName().Name); Factory = Configuration.BuildSessionFactory(); }