如何使用 NHibernate 流畅地映射没有 setter 和 backing 属性的属性?
假设我有以下实体:
public class CalculationInfo
{
public virtual Int64 Id { get; set; }
public virtual decimal Amount { get; set; }
public virtual decimal SomeVariable { get; set; }
public virtual decimal SomeOtherVariable { get; set; }
public virtual decimal CalculatedAmount
{
get
{
decimal result;
// do crazy stuff with Amount, SomeVariable and SomeOtherVariable
return result;
}
}
}
基本上我想使用 NHibernate 读取和写入数据库中的所有字段,但 CalculatedAmount
除外,我只想编写它 。
每个类似的问题和相应的答案都涉及为该值指定一个后备存储,在这种情况下我不会这样做
如何使用 Fluent NHibernate 来完成此任务?
谢谢!
更新:这是我尝试过的方法,以及它导致的错误:
这是我对属性的映射...
Map(x => x.CalculatedAmount)
.ReadOnly();
以及它产生的异常...
无法找到属性的设置器“xxx.CalculationInfo”类中的“CalculatedAmount”
Let's say I have the following entity:
public class CalculationInfo
{
public virtual Int64 Id { get; set; }
public virtual decimal Amount { get; set; }
public virtual decimal SomeVariable { get; set; }
public virtual decimal SomeOtherVariable { get; set; }
public virtual decimal CalculatedAmount
{
get
{
decimal result;
// do crazy stuff with Amount, SomeVariable and SomeOtherVariable
return result;
}
}
}
Basically I want to read and write all of the fields to my database with NHibernate with the exception of CalculatedAmount
, which I simply want to write and not read back in.
Every similar issue and corresponding answer has dealt with specifying a backing store for the value, which I won't have in this scenario.
How can I accomplish this using Fluent NHibernate?
Thanks!
UPDATE: Here's what I've tried, and the error it leads to:
Here's my mapping for the property...
Map(x => x.CalculatedAmount)
.ReadOnly();
And the exception it yields...
Could not find a setter for property 'CalculatedAmount' in class 'xxx.CalculationInfo'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现让这个映射在 Fluent NHibernate 中工作的方法是简单地添加 Access 属性:
I figured out that the way get this mapping working in Fluent NHibernate is to simply add the Access Property:
我不使用 Fluent,但在映射中,没有 setter 的持久属性使用
access="readonly"
进行映射,因此请查找类似.Readonly()
的内容(只读是从模型角度来看的;该值被写入数据库并用于脏检查)
I don't use Fluent, but in the mapping a persisted property with no setter is mapped with
access="readonly"
, so look for something like.Readonly()
(Readonly is from the model perspective; the value is written to the DB and used in dirty checks)
看起来这是一个计算值。如果您可以在任何给定时间计算该值,那么为什么还要存储它呢?
Looks like it's a calculated value. If you can calculate this value at any given time, then why store it at all?