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-idictionarystring-class-in-a-smart-way
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.