I cannot seem to change the value of a mapped property on an entity.
I have a simple parent / child relationship mapped fluently as
follows:
public class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Cache.ReadWrite();
DynamicUpdate();
Id(x => x.Id).GeneratedBy.GuidComb();
HasMany(x => x.GetChilds())
.AsSet()
.Inverse()
.Cascade.AllDeleteOrphan();
Map(x => x.Total);
}
}
public class ChildMap : ClassMap<Child>
{
public ChildMap()
{
Cache.ReadWrite();
DynamicUpdate();
Id(x => x.Id).GeneratedBy.GuidComb();
References(x => x.Parent);
Map(x => x.Total);
}
}
-----------------------------------------------------------
The classes themselves look like:
public class Parent : PersistentObject
{
private readonly ISet<Child> _childs = new HashedSet<Child>();
public virtual int Total { get; private set; }
protected internal virtual void UpdateTotal()
{
var newTotal = GetChilds().Sum(x => x.Total);
Total = newTotal;
}
public virtual Child AddChild(Child child)
{
_childs.Add(child);
child.Parent = this;
return child;
}
public virtual Child[] GetChilds()
{
return _childs.ToArray();
}
}
public class Child : PersistentObject
{
public virtual Parent Parent { get; set; }
public virtual int Total { get; private set; }
public virtual void ResetTotal(int newTotal)
{
Total = newTotal;
Parent.UpdateTotal();
}
}
-----------------------------------------------------------
The problem is, when I have loaded a Child from the database, and I
call ResetTotal() on the child, this should bubble up to calling
Parent.ResetChild(). In the course of that call on the Parent, I can
step through and see that Parent.Total is hitting the assignment
statement (Total = newTotal;), but this doesn't actually change the
value that is stored in Total.
Here are two test cases -- one that fails because it uses a persisted
and reloaded instance of a Child, and one that works because it uses
non-persistent instances:
[Test]
public void
Should_update_total_for_parent___fails_when_parent_is_proxied()
{
var parent = new Parent();
var child = parent.AddChild(new Child());
PersistEntities(parent);
using (var session = GetSession())
{
var loaded = session.Load<Child>(child.Id);
loaded.ResetTotal(3);
Parent referencedParent = loaded.Parent;
referencedParent.Total.ShouldEqual(3);
}
}
[Test]
public void
Should_update_total_for_parent___works_fine_for_non_persistent_entities
()
{
var parent = new Parent();
var child = parent.AddChild(new Child());
child.ResetTotal(3);
Parent referencedParent = child.Parent;
referencedParent.Total.ShouldEqual(3);
}
-----------------------------------------------------------
Is there some cache setting that I need to change? It just seems like
the ParentProxy is getting in the way and not properly wired up to the
underlying field on the auto property.
--
You received this message because you are subscribed to the Google Groups
"nhusers" 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/nhusers?hl=en.