NHibernate is making my collection read-only. How can I stop it?

I have a problem with Nhibernate / Fluent NHibernate

I have a class that has a collection and a backing field, and methods to manage a collection like this:

Edit: I added a virtual modifier in Children

since I forgot to paste 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 a 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();
    }
}

      

All is well now when I drop the MyClass instance from the database.

MyClass myClass = repo.GetById(12);

myClass.AddToChildren(new SomeChildObject());

      

This works great.

And then I make some changes and save the changes to the database.

Once the changes are saved, I then try to add another child

myClass.AddToChildren(new SomeChildObject("Another One!!!"));

      

And it crashes with "InvalidOperationException: assembly ReadOnly"

NHibernate seems to be doing something somewhere in this proxy. Does anyone know how to fix this problem?

Thanks in advance.

+2


source to share


3 answers


What happens if you disable lazy loading at the entity level? That is, make sure NHibernate is not using dynamic proxies for this class? (This does not affect the ability to lazy load the collection; lazy loading of the collection will still work).

In my applications, I use the same approach as the one you demonstrated here (a private field of a collection, a property that wraps around a collection and returns a read-only list) and I don't have the same problems you have. But the difference is that I declare in my mapping (I am not using fluent NHibernate, but plain old xml files :)) that NHibernate should not use dynamic proxies for my class:



 <class name="SomeClass" table="SomeTable" lazy="false">
      <!-- rest of mapping goes here -->
   </class>

      

+2


source


Considering the children are a private field, the proxy cannot modify it as the proxy can only use the front virtual fields and properties unless there is a way to set the children that are not in your code. This certainly looks very strange, but we will need to see the rest of the class that relates to this.



0


source


Your mapping tells nhibernate to use the Children public property, so it will not use the "children" private field.

I don't think you can specify private fields in nhibernate's freely mapped mappings, so if you want to do that, you should use an alternative mapping strategy like xml mapping files.

You can do the following to see if this is really the problem:

  • make the "children" field public (just check it out for now)
  • change the display to use the lowercase "private" field.
  • now see if the problem persists.
0


source







All Articles