views:

450

answers:

2

I have the a class similar to the following (nb! names have been changed to protect the innocent):

public class Person 
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual DateTime Birthday { get; set; }
    public virtual TimeSpan Age { get { return DateTime.Now - this.Birthday; } }
}

I use Fluent NHibernate to configure my mapping:

public class PersonMap : ClassMap<Person>
{
    public PersonMap() 
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Birthday);
    }
}

The problem is that this throws an exception:

Could not find a setter for property 'Age' in class 'Person'

If Age is not marked virtual I get:

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

Of course it cant find a setter and it shouldn't! How can I make this mapping work?

A: 

Don't make it virtual.

Daniel A. White
If it is not marked virtual I get:The following types may not be used as proxies:Person: method get_Age should be 'public/protected virtual' or 'protected internal virtual'
veggerby
+2  A: 

The real question to me is why is fluent NHibernate trying to map the Age property at all? It's not even in your mapping. I've only used earlier versions of fluent NHibernate, prior to the whole auto-mapping functionality, and never had this problem.

I suspect that either your Conventions are causing it to try to map Age, or you somehow have auto-mapping enabled which is conflicting with your manual mapping.

Also be aware that Fluent NHibernate somewhat recently changed conventions. So I would take a look at the following documentation:

http://wiki.fluentnhibernate.org/show/Conventions

http://wiki.fluentnhibernate.org/show/ConvertingToNewStyleConventions

http://wiki.fluentnhibernate.org/show/AutoMapping

Nathan
It WAS a convention that caused it... thanks.
veggerby
Hi Veggerby, can you elaborate on what convention was causing this? I have the same problem.
UpTheCreek
It was a Convention trying to specify my Column Names being Property Name + "Id" instead of Property Name + "_Id" (i.e. "PersonId" not "Person_Id"), but the way I did it did not work, so I just dropped the Convention and did it by "hand".
veggerby