8

I'm trying to complete some first step with fluent nhibernate with sql server express, to add this features in my project. But got some errors. As I'm newbie to nhibernate, couldn't solve a problem for some time. I have an entity with

Id { get; private set; }

accessors. And this entity is mapped to table with identity {1,1} column in sql server. But during creating Session factory, I get an error:

The following types may not be used as proxies:Entity: method set_Id should be 'public/protected virtual' or 'protected internal virtual'

I understand that private setter is used to encapsulate setting of this property, but why do I get this error then? PS: example on site of fluent for nhibernate is using sqllite db and everything is just fine.

2 Answers 2

20

This is an issue that has caused grief for many NH/FNH users, including me.

Until recently, NHibernate has allowed private setters with proxy objects. But starting with NH 3.2, private setters are no longer allowed - they must be "'public/protected virtual' or 'protected internal virtual'", as the error message says.

This is a breaking change for a lot of the FNH/NH sample code that's out there, and is especially confusing to newbies.

1
  • Marked your answer, it is closer to my question. Thanks.
    – Johnny_D
    Apr 20, 2012 at 14:49
7

Declare the property as public virtual int Id { get; protected set; }. The proxy object generated by NHibernate will then be able to set the property.

Another solution can be to use a backing field:

private int id;
public int Id
{
  get { return id; }
}

And then use mapping:

Map(x => x.Id).Access.CamelCaseField(); 
2
  • what for does this CamelCaseField is used? Does it manually realize autoproperty functinality?
    – Johnny_D
    Apr 23, 2012 at 10:45
  • The CamelCaseField instructs NHibernate to use reflection to set the private field, instead of a public property. Apr 24, 2012 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.