(Fluent) NHibernate mapping for class with calculated properties

╄→尐↘猪︶ㄣ 提交于 2019-12-24 03:28:09

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/1224541/fluent-nhibernate-mapping-for-class-with-calculated-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!