You have to set OwningEntity for Inverse to work. Got nuttin to do with your mappings. You've got to intercept the calls to the dictionary. Make your own indexer, or constrain access to the dictionary to a method on Entity, e.g. AddProperty(string, int). The possibilities are many.
PropertyMap refers to a property named Id. You've not got such a property. PropertyMap does not map Value. You probably want to do that. On Dec 31 2010, 4:08 pm, Marcin Seredynski <[email protected]> wrote: > NOTE: This is a copy of my post from StackOverflow, which is looking > deader than dead > lately.http://stackoverflow.com/questions/4564211/fluent-nhibernate-mapping-... > > Given these classes: > > using System.Collections.Generic; > > namespace FluentMappingsQuestion > { > public class Entity > { > public virtual int Id { get; set; } > public virtual IDictionary<string, Property> Properties { get; > set; } > } > > public class Property > { > public virtual Entity OwningEntity { get; set; } > public virtual string Name { get; set; } > public virtual int Value { get; set; } > } > } > > How can I map them using NHibernate (preferably fluent flavor) so that > doing this is possible: > > [Test] > public void EntityPropertyMappingTest() > { > using (var session = _factory.OpenSession()) > { > var entity = new Entity(); > > // (#1) I would *really* like to use this > entity.Properties["foo"] = 42; > > session.Save(entity); > session.Flush(); > > // (#2) I would like the entity below to "update > itself" > // on .Save or .Flush. I got it to work with .Load() > Assert.AreEqual(42, entity.Properties["foo"].Value); > Assert.AreEqual("foo", entity.Properties["foo"].Name); > Assert.AreEqual(entity, > entity.Properties["foo"].Owner); > } > } > > I have almost managed to do this with these mappings: > > // class EntityMap : ClassMap<Entity> > public EntityMap() > { > Id(x => x.Id); > HasMany(x => x.Properties) > .Cascade.AllDeleteOrphan() > .KeyColumn("EntityId") > .AsMap(x => x.Name); > } > > // class PropertyMap : ClassMap<Property> > public PropertyMap() > { > Id(x => x.Id); > References(x => x.OwningEntity).Column("EntityId"); > Map(x => x.Name).Length(32); > { > > The problems I have: > > * If I make `Entity.Properties` `.Inverse()`, it starts breaking > * If I don't make it `.Inverse()` then NHibernate does: > `INSERT(Entity), INSERT(Property), UPDATE(Property)` instead of just > `INSERT(Entity), INSERT(Property)` > * If I make `Property.Name` `.Not.Nullable()`, it starts breaking > * If I don't make it `.Not.Nullable()`, I have a hole in my db schema > > How should I change my mappings? -- You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.
