protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<Product>(cm => { cm.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); cm.Property(x => x.Name); cm.ManyToOne(x => x.Family); }); mapper.Class<Family>(cm => { cm.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); cm.Property(x => x.Name); cm.ManyToOne(x => x.Segment); cm.Set(x => x.Products, m => { }, m => m.OneToMany()); cm.Set(x => x.Cultivations, m => { }, m => m.OneToMany()); }); mapper.Class<Cultivation>(cm => { cm.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); cm.Property(x => x.Name); cm.ManyToOne(x => x.Family); }); mapper.Class<Segment>(cm => { cm.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); cm.Property(x => x.Name); cm.Set(x => x.Families, m => { }, m => m.OneToMany()); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
public void SpecifiedForeignKeyNameInByCodeMappingIsUsedInGeneratedSchema() { var mapper = new ModelMapper(); // Generates a schema in which a Person record cannot be created unless an Employee // with the same primary key value already exists. The Constrained property of the // one-to-one mapping is required to create the foreign key constraint on the Person // table, and the optional ForeignKey property is used to name it; otherwise a // generated name is used mapper.Class<Person>(rc => { rc.Id(x => x.Id, map => map.Generator(Generators.Foreign<Employee>(p => p.Person))); rc.Property(x => x.Name); rc.OneToOne(x => x.Employee, map => { map.Constrained(true); map.ForeignKey(ForeignKeyName); }); }); mapper.Class<Employee>(rc => { rc.Id(x => x.Id); rc.OneToOne(x => x.Person, map => { }); }); var script = new StringBuilder(); var cfg = new Configuration(); cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities()); new SchemaExport(cfg).Execute(s => script.AppendLine(s), false, false); script.ToString().Should().Contain(string.Format("constraint {0}", ForeignKeyName)); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<Employee>(mc => { mc.Id(x => x.Id, map => { map.Generator(Generators.Identity); map.Column("Id"); }); mc.OneToOne<EmployeeInfo>(x => x.Info, map => { map.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans); map.Constrained(false); }); }); mapper.Class<EmployeeInfo>(mc => { mc.Id(x => x.Id, map => { map.Generator(Generators.Foreign<EmployeeInfo>(x => x.EmployeeDetails)); map.Column("Id"); }); mc.OneToOne<Employee>(x => x.EmployeeDetails, map => { map.Constrained(true); }); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
private HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.BeforeMapClass += (mi, t, map) => map.Id(x => x.Generator(Generators.GuidComb)); mapper.Class<Parent>(rc => { rc.Id(p => p.Id); rc.Property(p => p.ParentCode, m => m.Unique(true)); rc.Property(p => p.Name); rc.Bag(p => p.Children, m => { m.Key(km => { km.Column(cm => cm.Name("ParentParentCode")); km.PropertyRef(pg => pg.ParentCode); }); m.Inverse(true); m.Cascade(Mapping.ByCode.Cascade.Persist); }, rel => rel.OneToMany()); }); mapper.Class<Child>(rc => { rc.Id(p => p.Id); rc.Property(p => p.Name); rc.ManyToOne<Parent>(p => p.Parent, m => { m.Column("ParentParentCode"); m.PropertyRef("ParentCode"); }); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<Author>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.Identity)); rc.Property(x => x.Name); rc.ManyToOne(x => x.Publisher, m => m.Cascade(Mapping.ByCode.Cascade.All)); rc.Set(x => x.Books, m => { m.Cascade(Mapping.ByCode.Cascade.All); m.Lazy(CollectionLazy.Lazy); }, r => r.OneToMany()); }); mapper.Class<Book>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.Identity)); rc.Property(x => x.Title); rc.ManyToOne(x => x.Author); }); mapper.Class<Publisher>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.Identity)); rc.Property(x => x.Name); rc.Set(x => x.Authors, m => m.Cascade(Mapping.ByCode.Cascade.All), r => r.OneToMany()); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<Order>(rc => { rc.Table("Orders"); rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); rc.Set(x => x.OrderLines, m => { m.Inverse(true); m.Key(k => { k.Column("OrderId"); k.NotNullable(true); }); m.Cascade(Mapping.ByCode.Cascade.All.Include(Mapping.ByCode.Cascade.DeleteOrphans)); m.Access(Accessor.NoSetter); }, m => m.OneToMany()); }); mapper.Class<OrderLine>(rc => { rc.Table("OrderLines"); rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); rc.ManyToOne(x => x.Order, m => m.Column("OrderId")); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<Parent>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); rc.List(x => x.Children, m => { m.Lazy(CollectionLazy.Extra); m.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans); m.Inverse(true); }, relation => relation.OneToMany()); }); mapper.Class<Child>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); rc.ManyToOne(x => x.Parent); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
private HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<User>(rt => { rt.Id(x => x.Id, map => map.Generator(Generators.Guid)); rt.Property(x => x.Name); rt.Set(x => x.Roles, map => { map.Table("UsersToRoles"); map.Inverse(true); map.Key(km => km.Column("UserId")); }, rel => rel.ManyToMany(mm => { mm.Column("RoleId"); mm.ForeignKey("FK_RoleInUser"); })); }); mapper.Class<Role>(rt => { rt.Id(x => x.Id, map => map.Generator(Generators.Guid)); rt.Property(x => x.Name); rt.Set(x => x.Users, map => { map.Table("UsersToRoles"); map.Key(km => km.Column("RoleId")); }, rel => rel.ManyToMany(mm => { mm.Column("UserId"); mm.ForeignKey("FK_UserInRole"); })); }); var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); return mappings; }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<Document>(rc => { rc.Id(x => x.Id, idMapper => idMapper.Generator(Generators.Identity)); rc.ManyToOne(x => x.Blob, m => { m.Cascade(Mapping.ByCode.Cascade.All); }); rc.Property(x => x.Name); }); mapper.Class<Blob>(map => { map.Id(x => x.Id, idMapper => idMapper.Generator(Generators.Identity)); map.Property(x => x.Bytes, y => { y.Column(x => { x.SqlType("varbinary(max)"); x.Length(int.MaxValue); }); y.Lazy(true); }); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
private static void Customize(ModelMapper mapper) { mapper.Class<EntityBase>(map => { map.Id(e => e.Id, id => id.Generator(Generators.HighLow)); map.Version(e => e.Version, d => { }); }); mapper.Class<Product>(map => map.Set(p => p.Customizations, set => set.Cascade(Cascade.All), rel => rel.ManyToMany())); mapper.Class<Customization>(map => map.Set(p => p.PossibleValues, set => set.Cascade(Cascade.All), rel => rel.Element())); mapper.Class<Order>(map => { map.Set(p => p.Items, set => { set.Cascade(Cascade.All); set.Inverse(true); }); map.ManyToOne(o => o.Payment, o => o.Cascade(Cascade.All)); }); }
protected override HbmMapping GetMappings() { // The impl/mapping of the bidirectional one-to-many sucks but was provided as is var mapper = new ModelMapper(); mapper.BeforeMapClass += (i, t, cm) => cm.Id(map => { map.Column((t.Name + "Id").ToUpperInvariant()); map.Generator(Generators.HighLow, g => g.Params(new { max_lo = "1000" })); }); mapper.Class<Customer>(ca => { ca.Lazy(false); ca.Id(x => x.Id, m => { }); ca.NaturalId(x => x.Property(c => c.Name, p => p.NotNullable(true))); ca.Property(x => x.Address, p => p.Lazy(true)); ca.Set(c => c.Orders, c => { c.Key(x => x.Column("CUSTOMERID")); c.Inverse(true); c.Cascade(Mapping.ByCode.Cascade.All); }, c => c.OneToMany()); }); mapper.Class<Order>(cm => { cm.Id(x => x.Id, m => { }); cm.Property(x => x.Date); cm.ManyToOne(x => x.Customer, map => map.Column("CUSTOMERID")); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<Employee>(mc => { mc.Id(x => x.Id, map => { map.Generator(Generators.Identity); map.Column("Id"); }); mc.ManyToOne<EmployeeInfo>(x => x.Info, map => { // Columns have to be declared first otherwise other properties are reset. map.Columns(x => { x.Name("COMP_ID"); }, x => { x.Name("PERS_ID"); }); map.Unique(true); map.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans); map.NotFound(NotFoundMode.Exception); }); }); mapper.Class<EmployeeInfo>(mc => { mc.ComponentAsId<EmployeeInfo.Identifier>(x => x.Id, map => { map.Property(x => x.CompanyId, m => m.Column("COMPS_ID")); map.Property(x => x.PersonId, m => m.Column("PERS_ID")); }); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.BeforeMapClass += (inspector, type, map) => map.Id(x=> x.Generator(Generators.GuidComb)); mapper.Class<Foo>(mc => { mc.Id(x => x.Id); mc.Bag(x => x.Bars, map => { map.Inverse(true); map.Cascade(Mapping.ByCode.Cascade.All); map.Key(km => { km.Column("FooId"); km.OnDelete(OnDeleteAction.Cascade); }); }, rel => rel.OneToMany()); }); mapper.Class<Bar>(mc => { mc.Id(x => x.Id); mc.ManyToOne(x=> x.Foo, map=> map.Column("FooId")); }); var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); return mappings; }
protected override void Mapping(Configuration config) { var mapper = new ModelMapper(); mapper.Class<Software>(map => { map.Id(s => s.Id, o => o.Generator(Generators.GuidComb)); map.Property(s => s.Name, o => { o.NotNullable(true); o.Unique(true); }); }); mapper.Class<AssignedSoftware>(map => { map.Id(s => s.Key, o => o.Generator(Generators.Assigned)); map.Property(s => s.Name, o => { o.NotNullable(true); o.Unique(true); }); }); var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities(); config.AddMapping(mapping); config.DataBaseIntegration(db => db.LogSqlInConsole = true); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<Employee>(mc => { mc.Id(x => x.Id, map => { map.Generator(Generators.Identity); map.Column("Id"); }); mc.ManyToOne<EmployeeInfo>(x => x.Info, map => { map.Column("Info_id"); map.Unique(true); map.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans); }); }); mapper.Class<EmployeeInfo>(cm => { cm.Id(x => x.Id, m => { m.Generator(Generators.Identity); m.Column("Id"); }); cm.OneToOne<Employee>(x => x.EmployeeDetails, map => { map.PropertyReference(x => x.Info); }); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
public override void InitDbModel(ModelMapper mapper) { mapper.Class<Dashboard>(cfg => cfg.Table("UniUI_Dashboard")); mapper.Class<Widget>(cfg => cfg.Table("UniUI_Widget")); mapper.Class<Panel>(cfg => cfg.Table("UniUI_Panel")); mapper.Class<WidgetParameter>(cfg => cfg.Table("UniUI_WidgetParameter")); }
public void Override(ModelMapper mapper) { mapper.Class<Movie>(map => map.Bag(x => x.Collectors, bag => { bag.Key(key => { key.Column("MovieFk"); }); bag.Table("Movies_Collectors"); bag.Cascade(Cascade.None); }, collectionRelation => collectionRelation.ManyToMany(m => m.Column("CollectorFk")))); mapper.Class<Movie>(map => map.Bag(x => x.Directors, bag => { bag.Key(key => { key.Column("MovieFk"); }); bag.Table("Movies_Directors"); bag.Cascade(Cascade.None); }, collectionRelation => collectionRelation.ManyToMany(m => m.Column("DirectorFk")))); mapper.Class<Movie>(map => map.Bag(x => x.Writers, bag => { bag.Key(key => { key.Column("MovieFk"); }); bag.Table("Movies_Writers"); bag.Cascade(Cascade.None); }, collectionRelation => collectionRelation.ManyToMany(m => m.Column("WriterFk")))); mapper.Class<Movie>(map => map.Bag(x => x.Genres, bag => { bag.Key(key => { key.Column("MovieFk"); }); bag.Table("Movies_Genres"); bag.Cascade(Cascade.None); }, collectionRelation => collectionRelation.ManyToMany(m => m.Column("GenreFk")))); }
public override void AlterConfiguration(NHibernate.Cfg.Configuration cfg) { ModelMapper mm = new ModelMapper(); mm.Class<FileSystemItem>(FileSystemItemCustomization); mm.Class<FileSystemChunk>(FileSystemChunkCustomization); var compiledMapping = mm.CompileMappingForAllExplicitlyAddedEntities(); cfg.AddDeserializedMapping(compiledMapping, "N2"); }
private IModelInspector GetConfiguredInspector() { var autoinspector = new SimpleModelInspector(); var mapper = new ModelMapper(autoinspector); mapper.Class<MyClass>(x => { }); mapper.Class<Related>(x => { }); mapper.Class<Bidirectional>(x => { }); return autoinspector; }
public void WhenRelatedDeclaredAsOneToOneThenIsNotManyToOne() { var autoinspector = new SimpleModelInspector(); var mapper = new ModelMapper(autoinspector); mapper.Class<AEntity>(map => map.OneToOne(a => a.B, x => { })); mapper.Class<BEntity>(x=> { }); var inspector = (IModelInspector)autoinspector; Assert.That(inspector.IsManyToOne(typeof(AEntity).GetProperty("B")), Is.False); }
public override void AlterConfiguration(NHibernate.Cfg.Configuration cfg) { ModelMapper mm = new ModelMapper(); mm.Class<Bucket>(BucketCustomization); mm.Class<Statistic>(StatisticCustomization); var compiledMapping = mm.CompileMappingForAllExplicitlyAddedEntities(); cfg.AddDeserializedMapping(compiledMapping, "N2"); }
public void TestGenerators() { var mapper = new ModelMapper(); mapper.Class<A>(e => { e.Id(c => c.Id, c => c.Generator(Generators.Counter)); }); mapper.Class<B>(e => { e.Id(c => c.Id, c => c.Generator(Generators.UUIDHex)); }); mapper.Class<C>(e => { e.Id(c => c.Id, c => c.Generator(Generators.UUIDString)); }); mapper.Class<D>(e => { e.Id(c => c.Id, c => c.Generator(Generators.Increment)); }); mapper.Class<E>(e => { e.Id(c => c.Id, c => c.Generator(Generators.Select)); }); mapper.Class<F>(e => { e.Id(c => c.Id, c => c.Generator(Generators.SequenceHiLo)); }); mapper.Class<G>(e => { e.Id(c => c.Id, c => c.Generator(Generators.SequenceIdentity)); }); mapper.Class<H>(e => { e.Id(c => c.Id, c => c.Generator(Generators.Table)); }); mapper.Class<I>(e => { e.Id(c => c.Id, c => c.Generator(Generators.TriggerIdentity)); }); var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities(); Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(A).Name).Id.generator.@class, Generators.Counter.Class); Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(B).Name).Id.generator.@class, Generators.UUIDHex.Class); Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(C).Name).Id.generator.@class, Generators.UUIDString.Class); Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(D).Name).Id.generator.@class, Generators.Increment.Class); Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(E).Name).Id.generator.@class, Generators.Select.Class); Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(F).Name).Id.generator.@class, Generators.SequenceHiLo.Class); Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(G).Name).Id.generator.@class, Generators.SequenceIdentity.Class); Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(H).Name).Id.generator.@class, Generators.Table.Class); Assert.AreEqual(hbmMapping.RootClasses.Single(x => x.Name == typeof(I).Name).Id.generator.@class, Generators.TriggerIdentity.Class); }
private HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class<MyRelated>(rm=> rm.Id(x=> x.Id)); mapper.Class<MyEntity>(rm => { rm.Id(x => x.Id); rm.Bag(x => x.Relateds, am => am.Persister<MyCollectionPersister>(), rel=> rel.OneToMany()); }); var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); return mappings; }
public void ExplicitColumnNameIsAlwaysMapped(string columnName) { var mapper = new ModelMapper(); mapper.Class<Foo>(cm => cm.Bag(x => x.Bars, bpm => { }, cer => cer.ManyToMany(mtmm => mtmm.Column(columnName)))); mapper.Class<Bar>(cm => cm.Id(x => x.Id)); var mapping = mapper.CompileMappingFor(new[] { typeof(Foo), typeof(Bar) }); var hbmClass = mapping.RootClasses.Single(x => x.Name == "Foo"); var hbmBag = hbmClass.Properties.OfType<HbmBag>().Single(); var hbmManyToMany = (HbmManyToMany)hbmBag.ElementRelationship; Assert.AreEqual(columnName, hbmManyToMany.column); }
public void ComponentMappingJustOnceDemo() { var mapper = new ModelMapper(); mapper.Component<Name>(comp => { comp.Property(name => name.First); comp.Property(name => name.Last); }); mapper.Component<Address>(comp => { comp.Property(address => address.CivicNumber); comp.Property(address => address.Street); }); mapper.Class<Person1>(cm => { cm.Id(person => person.Id, map => map.Generator(Generators.HighLow)); cm.Property(person => person.Test); cm.Component(person => person.Name, comp => { }); cm.Component(person => person.Address, comp => { }); }); var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities(); var hbmClass = hbmMapping.RootClasses[0]; var hbmComponents = hbmClass.Properties.OfType<HbmComponent>(); hbmComponents.Should().Have.Count.EqualTo(2); hbmComponents.Select(x => x.Name).Should().Have.SameValuesAs("Name","Address"); }
public static Configuration BuildConfiguration(string connStr) { var cfg = new Configuration(); // See http://fabiomaulo.blogspot.com/2009/07/nhibernate-configuration-through.html cfg.DataBaseIntegration(db => { db.Driver<SqlClientDriver>(); db.Dialect<MsSql2012Dialect>(); db.ConnectionString = connStr; // db.ConnectionStringName = "ConnStr"; db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'"; // See http://geekswithblogs.net/lszk/archive/2011/07/12/showing-a-sql-generated-by-nhibernate-on-the-vs-build-in.aspx //db.LogSqlInConsole = true; // Remove if using Log4Net //db.LogFormattedSql = true; //db.AutoCommentSql = true; db.SchemaAction = SchemaAutoAction.Validate; // This correspond to "hbm2ddl.validate", see http://nhforge.org/blogs/nhibernate/archive/2008/11/23/nhibernate-hbm2ddl.aspx }); var mapper = new ModelMapper(); mapper.Class<Parent>(map => { map.Id(x => x.Id, m => { m.Generator(Generators.GuidComb); m.UnsavedValue(Guid.Empty); }); map.Version(x => x.RowVersion, m => m.UnsavedValue(0)); map.Property(x => x.Description); }); cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities()); return cfg; }
public void WhenMapDynCompoAttributesThenMapAttributes() { var mapper = new ModelMapper(); mapper.Class<Person>(map => { map.Id(x => x.Id, idmap => { }); map.Component(x => x.Info, new { MyInt = 5}, z => { z.Access(Accessor.Field); z.Insert(false); z.Update(false); z.Unique(true); z.OptimisticLock(false); }); }); var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Person) }); var hbmClass = hbmMapping.RootClasses[0]; var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault(); hbmDynamicComponent.access.Should().Contain("field"); hbmDynamicComponent.insert.Should().Be.False(); hbmDynamicComponent.update.Should().Be.False(); hbmDynamicComponent.optimisticlock.Should().Be.False(); hbmDynamicComponent.unique.Should().Be.True(); }
public void TestMapElementElement() { var mapper = new ModelMapper(); mapper.Class<ClassWithMapElementElement>( c => { c.Lazy(false); c.Id(id => id.Id, id => id.Generator(Generators.Identity)); c.Map( m => m.Map, col => { col.Table("element_element"); col.Key(k => k.Column("id")); }, key => key.Element(e => e.Column("key")), element => element.Element(e => e.Column("element"))); }); var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); var hbmClass = mappings.RootClasses.FirstOrDefault(c => c.Name == typeof (ClassWithMapElementElement).Name); var hbmMap = hbmClass.Properties.OfType<HbmMap>().SingleOrDefault(); Assert.That(hbmMap, Is.Not.Null); Assert.That(hbmMap.Item, Is.TypeOf<HbmMapKey>()); Assert.That(hbmMap.Item1, Is.TypeOf<HbmElement>()); }
public void ShouldProperlyMapComponentWhenMappingOnlyPartOfItInSomePlaces() { var mapper = new ModelMapper(); mapper.Class<ClassWithComponents>(cm => { cm.Component(x => x.Component1, c => { c.Property(x => x.PropertyOne, p => p.Column("OnePropertyOne")); }); cm.Component(x => x.Component2, c => { c.Property(x => x.PropertyOne, p => p.Column("TwoPropertyOne")); c.Property(x => x.PropertyTwo, p => p.Column("TwoPropertyTwo")); }); }); //Compile, and get the component property in which we mapped only one inner property var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); var component1PropertyMapping = (HbmComponent)mappings.RootClasses[0].Properties.Single(x => x.Name == "Component1"); //There should be only one inner property in the mapping of this component // Note: take a look at how CURRENTLY the test fails with 1 expected vs 2, instead of vs 3. // This means that the "PropertyThree" property of the component that was never mapped, is not taken into account (which is fine). Assert.That(component1PropertyMapping.Items.Length, Is.EqualTo(1)); }
public void MapClassWithIdAndProperty() { var mapper = new ModelMapper(); mapper.Class<MyClass>(ca => { ca.Id("id", map => { map.Column("MyClassId"); map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 })); }); ca.Version("version", map => { }); ca.Property("something", map => map.Length(150)); }); var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) }); var hbmClass = hbmMapping.RootClasses[0]; hbmClass.Should().Not.Be.Null(); var hbmId = hbmClass.Id; hbmId.Should().Not.Be.Null(); hbmId.name.Should().Be("id"); hbmId.access.Should().Be("field"); var hbmGenerator = hbmId.generator; hbmGenerator.Should().Not.Be.Null(); [email protected]().Be("hilo"); hbmGenerator.param[0].name.Should().Be("max_low"); hbmGenerator.param[0].GetText().Should().Be("100"); var hbmVersion = hbmClass.Version; hbmVersion.name.Should().Be("version"); var hbmProperty = hbmClass.Properties.OfType<HbmProperty>().Single(); hbmProperty.name.Should().Be("something"); hbmProperty.access.Should().Be("field"); hbmProperty.length.Should().Be("150"); }
public override void InitDbModel(ModelMapper mapper) { mapper.Class <ReceivedData>(cfg => cfg.Table("Mqtt_ReceivedData")); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class <EntityComplex>( rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Version(ep => ep.Version, vm => { }); rc.Property(x => x.Name); rc.Property(ep => ep.LazyProp, m => m.Lazy(true)); rc.ManyToOne(ep => ep.SameTypeChild, m => m.Column("SameTypeChildId")); rc.ManyToOne(ep => ep.SameTypeChild2, m => m.Column("SameTypeChild2Id")); }); mapper.Class <EntityWithCompositeId>( rc => { rc.ComponentAsId( e => e.Key, ekm => { ekm.Property(ek => ek.Id1); ekm.Property(ek => ek.Id2); }); rc.Property(e => e.Name); }); mapper.Class <EntityWithCompositeId>( rc => { rc.ComponentAsId( e => e.Key, ekm => { ekm.Property(ek => ek.Id1); ekm.Property(ek => ek.Id2); }); rc.Property(e => e.Name); }); mapper.Class <EntityWithNoAssociation>( rc => { rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb)); rc.Property(e => e.Complex1Id); rc.Property(e => e.Complex2Id); rc.Property(e => e.Simple1Id); rc.Property(e => e.Simple2Id); rc.Property(e => e.Composite1Key1); rc.Property(e => e.Composite1Key2); rc.Property(e => e.CustomEntityNameId); }); mapper.Class <EntityCustomEntityName>( rc => { rc.EntityName(_customEntityName); rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb)); rc.Property(e => e.Name); }); return(mapper.CompileMappingForAllExplicitlyAddedEntities()); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class <EntityComplex>( rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Version(ep => ep.Version, vm => { }); rc.Property(x => x.Name); rc.Property(ep => ep.LazyProp, m => m.Lazy(true)); rc.ManyToOne(ep => ep.Child1, m => m.Column("Child1Id")); rc.ManyToOne(ep => ep.Child2, m => m.Column("Child2Id")); rc.ManyToOne(ep => ep.SameTypeChild, m => m.Column("SameTypeChildId")); rc.Bag( ep => ep.ChildrenList, m => { m.Cascade(Mapping.ByCode.Cascade.All); m.Inverse(true); }, a => a.OneToMany()); }); mapper.Class <EntitySimpleChild>( rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); }); mapper.Class <EntityWithCompositeId>( rc => { rc.ComponentAsId( e => e.Key, ekm => { ekm.Property(ek => ek.Id1); ekm.Property(ek => ek.Id2); }); rc.Property(e => e.Name); }); mapper.Class <EntityCustomEntityName>( rc => { rc.EntityName(customEntityName); rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); }); return(mapper.CompileMappingForAllExplicitlyAddedEntities()); }
public override void InitDbModel(ModelMapper mapper) { mapper.Class <UserScript>(cfg => cfg.Table("Scripts_UserScript")); mapper.Class <ScriptEventHandler>(cfg => cfg.Table("Scripts_EventHandler")); }
public override void InitDbModel(ModelMapper mapper) { mapper.Class <AlarmTime>(cfg => cfg.Table("AlarmClock_AlarmTime")); }
public void IdBag_InBaseEntity_WithDifferentTables_ShouldBeMappedAccordingly() { var mapper = new ModelMapper(); mapper.Class <Entity1>(cm => { cm.Table("Legacy1"); cm.Id(p => p.Id); cm.Property(p => p.Foo); cm.IdBag(p => p.ComponentCollection, m => { m.Access(Accessor.Field); m.Fetch(CollectionFetchMode.Select); m.Table("Legacy1_Comp"); }, m => m.Component(c => { c.Property(p => p.Property1); c.Property(p => p.Property2); })); }); mapper.Class <Entity2>(cm => { cm.Table("Legacy2"); cm.Id(p => p.Id); cm.Property(p => p.Bar); cm.IdBag(p => p.ComponentCollection, m => { m.Access(Accessor.Field); m.Fetch(CollectionFetchMode.Select); m.Table("Legacy2_Comp"); }, m => m.Component(c => { c.Property(p => p.Property1); c.Property(p => p.Property2); })); }); mapper.Class <Entity3>(cm => { cm.Id(p => p.Id); cm.IdBag(p => p.ComponentCollection, m => { m.Access(Accessor.Field); m.Fetch(CollectionFetchMode.Select); }, m => m.Component(c => { c.Property(p => p.Property1); c.Property(p => p.Property2); })); cm.Property(p => p.Baz); }); //set some defaults: //our bags are by default stored in a table called RootType_Component (unless overridden by the mapping) mapper.BeforeMapIdBag += (mi, m, pc) => pc.Table(m.GetRootMember().ReflectedType.Name + "_Component"); //all entities are by default stored in a table called Type mapper.BeforeMapClass += (mi, t, pc) => pc.Table(t.Name); var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); var classMappings = mappings.Items.OfType <HbmClass>(); Assert.That(classMappings.Count(), Is.EqualTo(3)); var e1Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity1).Name); Assert.That(e1Mapping, Is.Not.Null); Assert.That(e1Mapping.table, Is.EqualTo("Legacy1")); var e1Bag = e1Mapping.Items.OfType <HbmIdbag>().FirstOrDefault(); Assert.That(e1Bag, Is.Not.Null); Assert.That(e1Bag.Table, Is.EqualTo("Legacy1_Comp")); var e2Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity2).Name); Assert.That(e2Mapping, Is.Not.Null); Assert.That(e2Mapping.table, Is.EqualTo("Legacy2")); var e2Bag = e2Mapping.Items.OfType <HbmIdbag>().FirstOrDefault(); Assert.That(e2Bag, Is.Not.Null); Assert.That(e2Bag.Table, Is.EqualTo("Legacy2_Comp")); var e3Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity3).Name); Assert.That(e3Mapping, Is.Not.Null); Assert.That(e3Mapping.table, Is.EqualTo("Entity3")); var e3Bag = e3Mapping.Items.OfType <HbmIdbag>().FirstOrDefault(); Assert.That(e3Bag, Is.Not.Null); Assert.That(e3Bag.Table, Is.EqualTo("Entity3_Component")); }
public const int PERIOD = 36; // in hours public override void InitDbModel(ModelMapper mapper) { mapper.Class <TemperatureSensor>(cfg => cfg.Table("Microclimate_TemperatureSensor")); mapper.Class <TemperatureData>(cfg => cfg.Table("Microclimate_TemperatureData")); }
public override void InitDbModel(ModelMapper mapper) { mapper.Class <Location>(cfg => cfg.Table("Weather_Location")); mapper.Class <WeatherData>(cfg => cfg.Table("Weather_Data")); }
public override Configuration Map(Configuration cfg) { ModelMapper mapper = new ModelMapper(); mapper.Class <Address>(ca => { ca.Table("address"); ca.Lazy(true); ca.Id(x => x.CustomerId, map => { map.Column("customer_id"); map.Generator(Generators.Foreign <Address>(x => x.Customer)); }); ca.Property(x => x.City, x => { x.NotNullable(true); x.Column("city"); x.Length(50); }); ca.Property(x => x.Country, x => { x.NotNullable(true); x.Column("country"); x.Length(50); }); ca.Property(x => x.Street, x => { x.NotNullable(true); x.Column("street"); x.Length(50); }); ca.Property(x => x.ZipCode, x => { x.NotNullable(true); x.Column("zipcode"); x.Length(10); }); ca.OneToOne(x => x.Customer, x => { x.Constrained(true); }); }); mapper.Class <Customer>(ca => { ca.Table("customer"); ca.Lazy(true); ca.Id(x => x.CustomerId, map => { map.Column("customer_id"); map.Generator(Generators.HighLow); }); ca.NaturalId(x => x.Property(c => c.Name, y => { y.Column("name"); y.Length(50); y.NotNullable(true); })); ca.NaturalId(x => x.Property(c => c.Email, y => { y.Column("email"); y.Length(50); })); ca.Property(x => x.Name, p => { p.NotNullable(true); p.Column("name"); }); ca.Set(c => c.Orders, c => { c.Key(x => x.Column("customer_id")); c.Fetch(CollectionFetchMode.Select); c.Inverse(true); c.Lazy(CollectionLazy.Extra); c.Cascade(Cascade.All | Cascade.DeleteOrphans); }, c => c.OneToMany()); ca.Set(c => c.RecentOrders, c => { c.Key(x => x.Column("customer_id")); c.Fetch(CollectionFetchMode.Select); c.Where("(date >= (GETDATE() - 7))"); c.Inverse(true); c.Mutable(false); c.Cascade(Cascade.None); c.Lazy(CollectionLazy.Lazy); }, c => c.OneToMany()); ca.OneToOne(x => x.Address, x => { x.Cascade(Cascade.All | Cascade.DeleteOrphans); x.Constrained(false); }); }); mapper.Class <Order>(ca => { ca.Table("order"); ca.Lazy(true); ca.Id(x => x.OrderId, map => { map.Column("order_id"); map.Generator(Generators.HighLow); }); ca.Property(x => x.State, x => { x.NotNullable(true); x.Column("state"); x.Type <EnumType <OrderState> >(); }); ca.Property(x => x.Date, x => { x.NotNullable(true); x.Column("date"); }); ca.ManyToOne(c => c.Customer, a => { a.Column("customer_id"); a.NotNullable(true); a.Fetch(FetchKind.Select); a.Lazy(LazyRelation.NoProxy); a.Cascade(Cascade.All); }); ca.Set(x => x.Details, x => { x.Key(c => c.Column("order_id")); x.Fetch(CollectionFetchMode.Subselect); x.Inverse(true); x.Lazy(CollectionLazy.Extra); x.Cascade(Cascade.All | Cascade.DeleteOrphans); }, c => c.OneToMany()); }); mapper.Class <OrderDetail>(ca => { ca.Table("order_detail"); ca.Lazy(true); ca.Id(x => x.OrderDetailId, map => { map.Column("order_detail_id"); map.Generator(Generators.HighLow); }); ca.Property(x => x.Quantity, x => { x.NotNullable(true); x.Column("quantity"); }); ca.Property(x => x.ItemsPrice, x => { x.Formula("(quantity * (SELECT product.price FROM product WHERE product.product_id = product_id))"); x.Access(Accessor.None); x.Update(false); x.Type(NHibernateUtil.Decimal); x.Insert(false); }); ca.ManyToOne(x => x.Order, x => { x.NotNullable(true); x.Column("order_id"); x.Fetch(FetchKind.Select); x.Lazy(LazyRelation.NoProxy); x.Cascade(Cascade.None); }); ca.ManyToOne(x => x.Product, x => { x.NotNullable(true); x.Column("product_id"); x.Fetch(FetchKind.Select); x.Lazy(LazyRelation.NoProxy); x.Cascade(Cascade.None); }); }); mapper.Class <Product>(ca => { ca.Table("product"); ca.Lazy(true); ca.Id(x => x.ProductId, map => { map.Column("product_id"); map.Generator(Generators.HighLow); }); ca.NaturalId(x => x.Property(c => c.Name)); ca.Property(x => x.OrderCount, x => { x.Formula("(SELECT COUNT(1) FROM order_detail WHERE order_detail.product_id = product_id)"); }); ca.Property(x => x.Name, x => { x.NotNullable(true); x.Column("name"); }); ca.Property(x => x.Price, x => { x.NotNullable(true); x.Column("price"); }); ca.Property(x => x.Picture, x => { x.NotNullable(false); x.Column("picture"); x.Lazy(true); x.Type <ImageUserType>(new { ImageFormat = ImageFormat.Gif }); }); ca.Property(x => x.Specification, x => { x.NotNullable(true); x.Column("specification"); x.Lazy(true); x.Type <XDocType>(); }); ca.Set(x => x.OrderDetails, x => { x.Key(c => c.Column("product_id")); x.Inverse(true); x.Cascade(Cascade.All | Cascade.DeleteOrphans); x.Lazy(CollectionLazy.Lazy); }, c => c.OneToMany()); ca.Map(x => x.Attributes, c => { c.Cascade(Cascade.All); c.Lazy(CollectionLazy.Extra); c.Table("product_attribute"); c.Key(y => { y.Column("product_id"); y.NotNullable(true); }); }, k => { k.Element(e => { e.Column(y => { y.Name("name"); y.NotNullable(true); }); }); }, r => { r.Element(e => { e.Column("value"); e.NotNullable(true); }); }); }); mapper.AddMapping <PersonMapping>(); mapper.AddMapping <ForeignCitizenMapping>(); mapper.AddMapping <NationalCitizenMappping>(); mapper.AddMapping <LanguageMapping>(); mapper.AddMapping <TermMapping>(); mapper.AddMapping <TranslationMapping>(); mapper.AddMapping <RecordMapping>(); //for mapping by code mapper.AddMapping <BlogMapping>(); mapper.AddMapping <UserMapping>(); mapper.AddMapping <PostMapping>(); mapper.AddMapping <CommentMapping>(); mapper.AddMapping <AttachmentMapping>(); //mapper.AddMappings(typeof(BlogMapping).Assembly.GetTypes().Where(x => x.BaseType.IsGenericType && x.BaseType.GetGenericTypeDefinition() == typeof(ClassMapping<>))); HbmMapping mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); cfg.AddDeserializedMapping(mappings, null); return(cfg); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class <EntityComplex>( rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Version(ep => ep.Version, vm => { }); rc.Property(x => x.Name); rc.Property(ep => ep.LazyProp, m => m.Lazy(true)); rc.ManyToOne(ep => ep.SameTypeChild, m => m.Column("SameTypeChildId")); rc.ManyToOne(ep => ep.SameTypeChild2, m => m.Column("SameTypeChild2Id")); }); mapper.Class <EntityWithCompositeId>( rc => { rc.ComponentAsId( e => e.Key, ekm => { ekm.Property(ek => ek.Id1); ekm.Property(ek => ek.Id2); }); rc.Property(e => e.Name); }); mapper.Class <EntityWithCompositeId>( rc => { rc.ComponentAsId( e => e.Key, ekm => { ekm.Property(ek => ek.Id1); ekm.Property(ek => ek.Id2); }); rc.Property(e => e.Name); }); mapper.Class <EntityWithNoAssociation>( rc => { rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb)); rc.Property(e => e.Complex1Id); rc.Property(e => e.Complex2Id); rc.Property(e => e.Simple1Id); rc.Property(e => e.Simple2Id); rc.Property(e => e.Composite1Key1); rc.Property(e => e.Composite1Key2); rc.Property(e => e.CustomEntityNameId); }); mapper.Class <EntityCustomEntityName>( rc => { rc.EntityName(_customEntityName); rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb)); rc.Property(e => e.Name); }); mapper.Class <OneToOneEntity>( rc => { rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb)); rc.Property(e => e.Name); }); mapper.Class <PropRefEntity>( rc => { rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb)); rc.Property(e => e.Name); rc.Property(e => e.PropertyRef, m => m.Column("EntityPropertyRef")); }); mapper.Class <NullableOwner>( rc => { rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb)); rc.Property(e => e.Name); rc.OneToOne(e => e.OneToOne, m => m.Constrained(false)); rc.ManyToOne( e => e.PropRef, m => { m.Column("OwnerPropertyRef"); m.PropertyRef(nameof(PropRefEntity.PropertyRef)); m.ForeignKey("none"); m.NotFound(NotFoundMode.Ignore); }); rc.ManyToOne(e => e.ManyToOne, m => m.NotFound(NotFoundMode.Ignore)); }); return(mapper.CompileMappingForAllExplicitlyAddedEntities()); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class <EntityComplex>( rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Version(ep => ep.Version, vm => { }); rc.Property(x => x.Name); rc.Property(ep => ep.LazyProp, m => m.Lazy(true)); rc.ManyToOne(ep => ep.Child1, m => m.Column("Child1Id")); rc.ManyToOne(ep => ep.Child2, m => m.Column("Child2Id")); rc.ManyToOne(ep => ep.SameTypeChild, m => m.Column("SameTypeChildId")); rc.Bag( ep => ep.ChildrenList, m => { m.Cascade(Mapping.ByCode.Cascade.All); m.Inverse(true); }, a => a.OneToMany()); }); mapper.Class <EntitySimpleChild>( rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.ManyToOne(x => x.Parent); rc.Property(x => x.Name); }); mapper.Class <EntityEager>( rc => { rc.Lazy(false); rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); rc.Bag(ep => ep.ChildrenListSubselect, m => { m.Cascade(Mapping.ByCode.Cascade.All); m.Inverse(true); m.Fetch(CollectionFetchMode.Subselect); m.Lazy(CollectionLazy.NoLazy); }, a => a.OneToMany()); rc.Bag(ep => ep.ChildrenListEager, m => { m.Lazy(CollectionLazy.NoLazy); }, a => a.OneToMany()); }); mapper.Class <EntitySubselectChild>( rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); rc.ManyToOne(c => c.Parent); }); return(mapper.CompileMappingForAllExplicitlyAddedEntities()); }
public override void InitDbModel(ModelMapper mapper) { mapper.Class <Dashboard>(cfg => cfg.Table("UniUI_Dashboard")); mapper.Class <Widget>(cfg => cfg.Table("UniUI_Widget")); mapper.Class <WidgetParameter>(cfg => cfg.Table("UniUI_WidgetParameter")); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); #region Subclass hierarchy mapper.Class <EntityClassProxy>( rc => { rc.Id(x => x.Id); rc.Property(x => x.Name); }); mapper.UnionSubclass <SubEntityInterfaceProxy>( rc => { rc.Proxy(typeof(ISubEntityProxy)); rc.Property(x => x.AnotherName); }); mapper.UnionSubclass <AnotherSubEntityInterfaceProxy>( rc => { rc.Proxy(typeof(IAnotherEntityProxy)); rc.Property(x => x.AnotherName); }); mapper.Class <EntityWithSuperClassInterfaceLookup>( rc => { rc.Table("ClassWithInterfaceLookup"); rc.Id(x => x.Id); rc.Property(x => x.Name); rc.ManyToOne(x => x.EntityLookup, x => x.Class(typeof(EntityClassProxy))); }); #endregion Subclass hierarchy mapper.Class <EntitySimple>( rc => { rc.Id(x => x.Id); rc.Property(x => x.Name); }); mapper.Class <EntityExplicitInterface>( rc => { rc.Id(x => x.Id); rc.Property(x => x.Name); }); mapper.Class <EntityMultiInterfaces>( rc => { rc.Id(x => x.Id); rc.Property(x => x.Name); }); mapper.Class <EntityMixExplicitImplicitInterface>( rc => { rc.Table("multiInterface"); rc.Id(x => x.Id); rc.Property(x => x.Name); }); mapper.Class <EntityMultiIdProxy>( rc => { rc.Proxy(typeof(IMultiIdProxy)); rc.Id(x => x.Id); rc.Property(x => x.Name); }); mapper.Class <EntityWithExplicitGenericInterface>( rc => { rc.Table("explGenInterface"); rc.Id(x => x.Id); }); return(mapper.CompileMappingForAllExplicitlyAddedEntities()); }
/// <summary> /// 类型配置 /// </summary> static void Mapping() { ModelMapper mapper = new ModelMapper(); mapper.Class <Class>(m => { m.Cache(ch => ch.Usage(CacheUsage.ReadWrite)); m.Id <UInt32>(c => c.Id, im => { im.Generator(Generators.Native); im.Column("Id"); }); m.Property <string>(c => c.Name, im => im.Column("Name")); m.Property <string>(c => c.Slogan); m.Table("t_Class"); m.SchemaAction(SchemaAction.Export | SchemaAction.Update); m.Bag <Student>(c => c.Students, bm => { bm.BatchSize(45); bm.Lazy(CollectionLazy.Lazy); //bm.Fetch(CollectionFetchMode.Select); bm.Inverse(true); bm.Key(km => { km.Column("ClassId"); //km.PropertyRef<UInt32>(pg => pg.Id); }); //bm.Cascade(Cascade.Persist | Cascade.Remove | Cascade.DeleteOrphans); bm.Cascade(NHibernate.Mapping.ByCode.Cascade.Persist | NHibernate.Mapping.ByCode.Cascade.Remove); }, er => er.OneToMany()); }); mapper.Class <Student>(m => { m.Cache(ch => ch.Usage(CacheUsage.ReadWrite)); m.Table("t_Student"); m.Id <UInt32>(c => c.Id, im => { im.Generator(Generators.Native); im.Column("Id"); }); m.Property <string>(c => c.Name); m.Property <Int32>(c => c.Age); //m.Property<Int32>(c => c.ClassId); m.SchemaAction(SchemaAction.Export | SchemaAction.Update); m.ManyToOne <Class>(p => p.Class, m2m => { m2m.Column("ClassId"); m2m.Lazy(LazyRelation.Proxy); m2m.Class(typeof(Class)); //m2m.Cascade(Cascade.None); //m2m.PropertyRef("Id"); }); }); var hbmmap = mapper.CompileMappingForAllExplicitlyAddedEntities(); Console.WriteLine(hbmmap.AsString()); UnitOfWork.Configuration.AddMapping(hbmmap); //使用二次缓存 //config.EntityCache<Class>(ch => //{ // ch.Strategy = EntityCacheUsage.ReadWrite; // ch.RegionName = "asd"; // ch.Collection(c => c.Students, chc => { chc.Strategy = EntityCacheUsage.ReadWrite; chc.RegionName = "asdCollection"; }); //}); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class <EntityEager>( rc => { rc.Lazy(false); rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Version(ep => ep.Version, vm => { }); rc.Property(x => x.Name); MapList(rc, p => p.ChildrenList, CollectionFetchMode.Join); }); MapSimpleChild <EntityEagerChild>( mapper, rc => { rc.Lazy(false); }); mapper.Class <EntityComplex>( rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Version(ep => ep.Version, vm => { }); rc.Property(x => x.Name); rc.Property(ep => ep.LazyProp, m => m.Lazy(true)); rc.ManyToOne( ep => ep.Child1, m => { m.Column("Child1Id"); m.ForeignKey("none"); }); rc.ManyToOne( ep => ep.Child2, m => { m.Column("Child2Id"); m.ForeignKey("none"); }); rc.ManyToOne(ep => ep.SameTypeChild, m => { m.Column("SameTypeChildId"); m.ForeignKey("none"); }); MapList(rc, ep => ep.ChildrenList, mapper: m => m.OrderBy("OrderIdx desc")); MapList(rc, ep => ep.ChildrenListEmpty); }); MapSimpleChild( mapper, default(EntitySimpleChild), c => c.Children, rc => { rc.Property(sc => sc.LazyProp, mp => mp.Lazy(true)); rc.Property(sc => sc.OrderIdx); }); MapSimpleChild(mapper, default(Level2Child), c => c.Children); MapSimpleChild <Level3Child>(mapper); return(mapper.CompileMappingForAllExplicitlyAddedEntities()); }
public void PropertyCustomizerDifferentiatesBetweenChildClasses() { var mapper = new ModelMapper(); mapper.Class <OwnerChildOne>(classMapper => { classMapper.Id(p => p.Id); classMapper.Bag <OwnedItem> ( parent => parent.OwnedItems, bagPropertyMapper => { bagPropertyMapper.Table("ChildOne"); bagPropertyMapper.Key(k => k.Column("Parent_Id")); }, r => r.Component( child => { child.Property ( c => c.NameOnClient, pm => { pm.Column("OwnerChildOne_CustomColumnName"); } ); } ) ); }); mapper.Class <OwnerChildTwo>(classMapper => { classMapper.Id(p => p.Id); classMapper.Bag <OwnedItem> ( parent => parent.OwnedItems, bagPropertyMapper => { bagPropertyMapper.Table("ChildTwo"); bagPropertyMapper.Key(k => k.Column("Parent_Id")); }, r => r.Component( child => { child.Property ( c => c.NameOnClient, pm => { pm.Column("OwnerChildTwo_CustomColumnName"); } ); } ) ); }); var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); HbmBag bag1 = mappings .Items.Cast <HbmClass>() .Where(c => c.Name == typeof(OwnerChildOne).FullName) .Single() .Properties.Cast <HbmBag>() .Single(); HbmCompositeElement childElement1 = (HbmCompositeElement)bag1.Item; HbmProperty propertyMapping1 = childElement1.Properties.Cast <HbmProperty>().Single(); Assert.That(propertyMapping1.Columns.Single().name, Is.EqualTo("OwnerChildOne_CustomColumnName")); HbmBag bag2 = mappings .Items.Cast <HbmClass>() .Where(c => c.Name == typeof(OwnerChildTwo).FullName) .Single() .Properties.Cast <HbmBag>() .Single(); HbmCompositeElement childElement2 = (HbmCompositeElement)bag2.Item; HbmProperty propertyMapping2 = childElement2.Properties.Cast <HbmProperty>().Single(); Assert.That(propertyMapping2.Columns.Single().name, Is.EqualTo("OwnerChildTwo_CustomColumnName")); }
public static HbmMapping GetMapping() { var mapper = new ModelMapper(); mapper.Component <Address>(comp => { comp.Property(address => address.Street); comp.Property(address => address.City); comp.Property(address => address.PostalCode); comp.Property(address => address.Country); comp.ManyToOne(address => address.StateProvince); }); mapper.Class <Animal>(rc => { rc.Id(x => x.Id, map => map.Generator(Generators.Native)); rc.Property(animal => animal.Description); rc.Property(animal => animal.BodyWeight); rc.ManyToOne(animal => animal.Mother); rc.ManyToOne(animal => animal.Father); rc.ManyToOne(animal => animal.Zoo); rc.Property(animal => animal.SerialNumber); rc.Set(animal => animal.Offspring, cm => cm.OrderBy(an => an.Father), rel => rel.OneToMany()); }); mapper.JoinedSubclass <Reptile>(jsc => { jsc.Property(reptile => reptile.BodyTemperature); }); mapper.JoinedSubclass <Lizard>(jsc => { }); mapper.JoinedSubclass <Mammal>(jsc => { jsc.Property(mammal => mammal.Pregnant); jsc.Property(mammal => mammal.Birthdate); }); mapper.JoinedSubclass <DomesticAnimal>(jsc => { jsc.ManyToOne(domesticAnimal => domesticAnimal.Owner); }); mapper.JoinedSubclass <Cat>(jsc => { }); mapper.JoinedSubclass <Dog>(jsc => { }); mapper.JoinedSubclass <Human>(jsc => { jsc.Component(human => human.Name, comp => { comp.Property(name => name.First); comp.Property(name => name.Initial); comp.Property(name => name.Last); }); jsc.Property(human => human.NickName); jsc.Property(human => human.Height); jsc.Property(human => human.IntValue); jsc.Property(human => human.FloatValue); jsc.Property(human => human.BigDecimalValue); jsc.Property(human => human.BigIntegerValue); jsc.Bag(human => human.Friends, cm => { }, rel => rel.ManyToMany()); jsc.Map(human => human.Family, cm => { }, rel => rel.ManyToMany()); jsc.Bag(human => human.Pets, cm => { cm.Inverse(true); }, rel => rel.OneToMany()); jsc.Set(human => human.NickNames, cm => { cm.Lazy(CollectionLazy.NoLazy); cm.Sort(); }, cer => { }); jsc.Map(human => human.Addresses, cm => { }, rel => rel.Component(comp => { })); }); mapper.Class <User>(rc => { rc.Id(u => u.Id, im => im.Generator(Generators.Foreign <User>(u => u.Human))); rc.Property(user => user.UserName); rc.OneToOne(user => user.Human, rm => rm.Constrained(true)); rc.List(user => user.Permissions, cm => { }, cer => { }); }); mapper.Class <Zoo>(rc => { rc.Id(x => x.Id, map => map.Generator(Generators.Native)); rc.Property(zoo => zoo.Name); rc.Property(zoo => zoo.Classification); rc.Map(zoo => zoo.Mammals, cm => { }, rel => rel.OneToMany()); rc.Map(zoo => zoo.Animals, cm => { cm.Inverse(true); }, rel => rel.OneToMany()); rc.Component(zoo => zoo.Address, comp => { }); }); mapper.Subclass <PettingZoo>(sc => { }); mapper.Class <StateProvince>(rc => { rc.Id(x => x.Id, map => map.Generator(Generators.Native)); rc.Property(sp => sp.Name); rc.Property(sp => sp.IsoCode); }); return(mapper.CompileMappingFor(typeof(Animal).Assembly.GetTypes().Where(t => t.Namespace == typeof(Animal).Namespace))); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class <Client>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); }); mapper.JoinedSubclass <CorporateClient>(rc => { rc.Property(x => x.CorporateId); }); mapper.Class <Project>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); rc.Property(x => x.EmailPref, m => m.Type <EnumType <EmailPref> >()); rc.ManyToOne(x => x.Client, m => m.Column("ClientId")); rc.ManyToOne(x => x.BillingClient, m => m.Column("BillingClientId")); rc.ManyToOne(x => x.CorporateClient, m => m.Column("CorporateClientId")); rc.Set(x => x.Issues, m => { m.Key(k => k.Column(c => c.Name("ProjectId"))); }, rel => rel.OneToMany()); }); mapper.Class <Issue>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); rc.ManyToOne(x => x.Project, m => m.Column("ProjectId")); rc.ManyToOne(x => x.Client, m => m.Column("ClientId")); }); mapper.Class <Invoice>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.InvoiceNumber); rc.ManyToOne(x => x.Project, m => m.Column("ProjectId")); rc.ManyToOne(x => x.Issue, m => m.Column("IssueId")); }); mapper.Class <Employee>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); rc.Property(x => x.Name); rc.Property(x => x.ReviewAsPrimary); rc.Set(x => x.Projects, m => { m.Table("EmployeesToProjects"); m.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans); m.Key(k => k.Column(c => c.Name("EmployeeId"))); }, rel => rel.ManyToMany(m => m.Column("ProjectId"))); rc.Set(x => x.WorkIssues, m => { m.Table("EmployeesToWorkIssues"); m.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans); m.Key(k => k.Column(c => c.Name("EmployeeId"))); }, rel => rel.ManyToMany(m => m.Column("IssueId"))); rc.Set(x => x.ReviewIssues, m => { m.Table("EmployeesToReviewIssues"); m.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans); m.Key(k => k.Column(c => c.Name("EmployeeId"))); }, rel => rel.ManyToMany(m => m.Column("IssueId"))); }); return(mapper.CompileMappingForAllExplicitlyAddedEntities()); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.Class <Concept>(m => { m.Lazy(false); m.Id(c => c.Id, id => { id.Column("concept_id"); id.Generator(Generators.Native); }); m.Set(c => c.Mappings, collection => { collection.Fetch(CollectionFetchMode.Subselect); collection.Lazy(CollectionLazy.NoLazy); collection.Table("concept_mapping"); collection.Key(key => { key.Column("concept_id"); }); }, element => { element.Component(component => { component.Property(c => c.Relationship, p => p.Column("relationship")); component.ManyToOne(c => c.Code, manyToOne => { manyToOne.Column("code_id"); manyToOne.Fetch(FetchKind.Join); manyToOne.Cascade(NHibernate.Mapping.ByCode.Cascade.None); }); }); }); }); mapper.Class <ConceptCode>(m => { m.Table("concept_code"); m.Lazy(false); m.Id(c => c.Id, id => { id.Column("code_id"); id.Generator(Generators.Native); }); m.Property(c => c.CodeSource, p => { p.Column("source"); }); m.Property(c => c.Value, p => { p.Column("`value`"); }); }); return(mapper.CompileMappingForAllExplicitlyAddedEntities()); }
public override void InitDbModel(ModelMapper mapper) { base.InitDbModel(mapper); mapper.Class <Item>(cfg => cfg.Table("Storage_Item")); }
public override void InitDbModel(ModelMapper mapper) { mapper.Class <Tile>(cfg => cfg.Table("WebUI_Tile")); }
public override void InitDbModel(ModelMapper mapper) { mapper.Class <VoiceCommand>(cfg => cfg.Table("Speech_VoiceCommand")); }
private void MapSpotifySession() { _modelMapper.Class <SpotifySession>(e => { e.Id(p => p.Id, p => p.Generator(Generators.GuidComb)); e.Property(p => p.DeviceId, p => p.NotNullable(true)); e.Property(p => p.Time, p => p.NotNullable(true)); e.ManyToOne(p => p.Token, mapper => { mapper.Column("TokenId"); mapper.NotNullable(true); mapper.Cascade(Cascade.None); }); }); }