如何使用 NHibernate 流畅地映射没有 setter 和 backing 属性的属性?

发布于 2024-09-07 07:15:47 字数 947 浏览 4 评论 0原文

假设我有以下实体:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静 2024-09-14 07:15:47

我发现让这个映射在 Fluent NHibernate 中工作的方法是简单地添加 Access 属性:

Map(x => x.CalculatedAmount).Access.ReadOnly();

I figured out that the way get this mapping working in Fluent NHibernate is to simply add the Access Property:

Map(x => x.CalculatedAmount).Access.ReadOnly();
回眸一遍 2024-09-14 07:15:47

我不使用 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)

も让我眼熟你 2024-09-14 07:15:47

看起来这是一个计算值。如果您可以在任何给定时间计算该值,那么为什么还要存储它呢?

Looks like it's a calculated value. If you can calculate this value at any given time, then why store it at all?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文