tags:

views:

158

answers:

2

Hi All,

I have a domain object that has been auto generated for me by MyGeneration. This is generated using an NHbernate template. This is part of the object - I have removed most of it,

[Serializable]
public class Purchase : INotifyPropertyChanged
{
    protected int id;

    public event PropertyChangedEventHandler PropertyChanged;

    public virtual int Id
    {
        get { return id; }
        set {if (value != this.id){id= value;NotifyPropertyChanged("Id");}}
    }
}

When I try to save one of these objects into the database I get an exception

NHibernate.InvalidProxyTypeException: The following types may not be used as proxies: NHibernateDemo.DataLayer.Entities.Purchase: method add_PropertyChanged should be 'public/protected virtual'

Etc. So if I change this line

public event PropertyChangedEventHandler PropertyChanged;

To this ...

public virtual event PropertyChangedEventHandler PropertyChanged;

It works, but to me this doesn't seem like a great solution. NHibernate is treating that event property like it is a field it is going to persist into the database. Is there a way I can tell NHibernate to ignore it?

If I make it a 'virtual event' do you think WPF binding will still work with it?

+1  A: 

The exception means that NHibernate can't create a dynamic proxy of the object because the event is not virtual. NH creates a proxy of your object to allow for lazy loading. Your solution is fine.

Jamie Ide
My solution is fine? So making it virtual will work fine with WPF binding is that what you are saying?
peter
You need to make it virtual for NHibernate's lazy loading strategy to work. I expect that will work fine with WPF binding but I don't have any experience with it. The declarations have to be virtual so that NH can create a proxy for your object; databinding will work with the proxy as if it were the real object.
Jamie Ide
+1  A: 

You must be explicitly mapping this property in your NHibernate mapping file. Take a look at the generated hbm.xml file and remove this property from the mapping file so NHibernate will ignore it.

lomaxx
Are you saying I must be mapping this field for this particular error to occur? That doesn't seem to be the case. In my mapping files there is no mapping to the PropertyChanged event
peter