Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Nhibernate and Sql Server private setter error

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.

like image 279
Johnny_D Avatar asked Apr 19 '12 11:04

Johnny_D


2 Answers

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.

like image 79
Tom Bushell Avatar answered Nov 07 '22 17:11

Tom Bushell


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(); 
like image 26
Espen Burud Avatar answered Nov 07 '22 17:11

Espen Burud