(流利)与计算属性的NHibernate映射 [英] (Fluent) NHibernate mapping for class with calculated properties

查看:97
本文介绍了(流利)与计算属性的NHibernate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于下面的类(nb!名称已经被改为保护无辜):

  public class Person 
{
public virtual int Id {get;私人设置}
public virtual string Name {get;组; }
public virtual DateTime Birthday {get;组; }
public virtual TimeSpan Age {get {return DateTime.Now - this.Birthday;我使用Fluent NHibernate来配置我的映射:$ / $ p






$ b

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




$ b $ p
$ b

问题在于这会引发异常: p>


无法找到属性
'Age'的类名为'Person'的setter




如果Age没有标记为虚拟的,我会得到:

lockquote

以下类型不能使用as
proxies:Person:method get_Age should
''public / protected virtual'or
'protected internal virtual'

当然它找不到一个setter,它不应该!我怎样才能使这个映射工作?

解决方案

真正的问题是为什么是流利的NHibernate试图映射年龄属性所有?这甚至不在你的映射中。在整个自动映射功能之前,我只使用了早期版本的流利NHibernate,并且从来没有这个问题。



我怀疑是你的约定会导致它试图映射Age,或者你以某种方式启用了自动映射,这与你的手动映射有冲突。



另外请注意,Fluent NHibernate在某种程度上最近改变了惯例。所以我会看看下面的文档:
$ b



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

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


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?

解决方案

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

这篇关于(流利)与计算属性的NHibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆