Hi guys, I'm having a bit of an issue with Nhibernate / Fluent NHibernate
I have a class that has a collection and a backing field, and methods for manipulating the collection like so:
Edit: I've added the virtual modifier to Children
since I forgot to stick it in the example code below (it was 2 am)
public class MyClass
{
private IList<SomeChildObject> children;
public virtual IList<SomeChildObject> Children { get { return new ReadOnlyCollection<SomeChildObject>(children); } }
public void AddToChildren(SomeChildObject obj)
{
children.Add(obj);
}
}
And I have my Fluent NHibernate mapping like this:
public class MyClassMapping : ClassMap<MyClass>
{
public MyClassMapping()
{
HasMany(x => x.Children)
.Inverse()
.LazyLoad()
.Cascade.AllDeleteOrphan()
.KeyColumnNames.Add("MyClassID")
.Access.AsReadOnlyPropertyThroughCamelCaseField();
}
}
Now all is good when I pull back an instance of MyClass from the database.
MyClass myClass = repo.GetById(12);
myClass.AddToChildren(new SomeChildObject());
This works fine.
And then I make some changes and persist the changes to the database.
Once the changes have been saved, I then try and add another child object
myClass.AddToChildren(new SomeChildObject("Another One!!!"));
And it falls over with "InvalidOperationException: The Collection is ReadOnly"
Seems the NHibernate is doing something somewhere in it's proxy. Does anyone know how to resolve this issue?
Thanks in advance.