gpt4 book ai didi

c# - (流利的)具有计算属性的类的 NHibernate 映射

转载 作者:行者123 更新时间:2023-11-30 21:24:25 26 4
gpt4 key购买 nike

我有一个类似于以下的类(注意!名称已更改以保护无辜者):

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; } }
}

我使用 Fluent NHibernate 配置我的映射:

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

问题是这会引发异常:

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

如果 Age 没有标记为 virtual 我得到:

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

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

最佳答案

我真正的问题是,为什么 Fluent NHibernate 会尝试映射 Age 属性?它甚至不在您的映射中。在整个自动映射功能之前,我只使用过 fluent NHibernate 的早期版本,并且从来没有遇到过这个问题。

我怀疑您的约定导致它尝试映射年龄,或者您以某种方式启用了自动映射,这与您的手动映射相冲突。

另请注意,Fluent NHibernate 最近在某种程度上改变了约定。所以我会看一下以下文档:

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

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

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

关于c# - (流利的)具有计算属性的类的 NHibernate 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1224541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com