经过投影,我微风中未映射的属性似乎不起作用 [英] My unmapped properties in breeze does not seems to work whith a projection

查看:83
本文介绍了经过投影,我微风中未映射的属性似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体:

public class Invoice
{
    [Key]
    public int Id { get; set; }
    public DateTime? ArchiveDate { get; set; }
    public DateTime? ClotureDate { get; set; }
    ...
}

我想知道我的发票是否通过使用一种标志(布尔值)来存档或关闭。为此,我在微风实体中添加了2个未映射的属性,如下所示:

I would like to know whether my invoice is archived or closed by using a kind of flag (boolean). For that purpose I added 2 unmapped properties in my breeze entity like this:

public class Invoice
{
    [Key]
    public int Id { get; set; }
    public DateTime? ArchiveDate { get; set; }
    public DateTime? ClotureDate { get; set; }
    [NotMapped]
    public bool Archived { get { return ArchiveDate.HasValue; } } 
    [NotMapped]
    public bool Clotured { get { return ClotureDate.HasValue; } } 
    ...
}

现在我可以查询微风实体了像这样:

Now I can query my breeze entity like this:

var query = entityQuery.from("Invoices")
                       .where('id', '==', id)
                       .toType('Invoice');

以上调用将返回我的发票实体的所有属性(包括已归档和已归档)。效果很好。

The call above will return all properties of my invoice entity (including archived & clotured). It works well.

但是我只需要几个特定的​​属性(用于性能)。然后我尝试:

But I need only a few specific properties (for performance). Then I try:

var query = entityQuery.from("Invoices")
                       .where('id', '==', id)
                       .select("id, archived, clotured")
                       .toType('Invoice');

我收到错误: LINQ中不支持指定的类型成员 Archived实体。仅支持初始化程序,实体成员和实体导航属性。

非常令人沮丧。知道为什么我不能执行这样的查询吗?

Very frustrating. Any idea why do I cannot perform such query?

或者也许有人有其他解决方案?

Or maybe does someone have another solution?

非常感谢。

推荐答案

简短版


您所看到的完全是期望的。 ArchivedDate 既是持久数据属性又是序列化属性。 Archived 属性不会保留,但会被序列化。这就是为什么您查看同时为 ArchivedDate Archived 的数据值的原因。但是,您的远程查询 ...在服务器上执行的LINQ查询...可能仅引用持久属性,例如 ArchivedDate 。 EF对诸如 Archived 之类的计算属性一无所知;他们不能参加LINQ查询...而不是 where select orderBy 或任何其他查询。您不能在查询中提及EF不知道的内容...,而您(正确地)告诉EF忽略了这些已存档封闭的计算属性。

Short version

What you are seeing is perfectly expected. The ArchivedDate is both a persisted data property and a serialized property. The Archived property is not persisted but it is serialized. That's why you see data values for both ArchivedDate and Archived. However, your remote query ... the LINQ query executed on the server ... may only refer to the persisted properties such as ArchivedDate. EF knows nothing about calculated properties such as Archived; they cannot participate in a LINQ query ... not in a where, select, orderBy or any other query. You can't mention something in a query that EF doesn't know about ... and you told EF (properly) to ignore these Archived and Clotured calculated properties.

[Unmapped]属性从EF中隐藏属性...必须,因为已存档已分组是计算的属性,而不是持久性数据。

The [Unmapped] attribute hides the properties from EF ... as it must because Archived and Clotured are calculated properties, not persistable data.

[Unmapped]属性还会从EF生成的元数据中隐藏这些属性。

The [Unmapped] attribute also hides these properties from the metadata generated from EF. That too is both expected and good.

但是这也意味着您不能构造引用这些属性的LINQ查询。它们不是数据属性。 EF无法查询它们。 LINQ查询中只能显示数据属性和导航属性。真的就是这么简单。

But this also means that you cannot construct a LINQ query that references these properties. They aren't data properties. They can't be queried by EF. Only data properties and navigation properties can appear in a LINQ query. It is really that simple.

也许您想知道为什么未映射的计算出的属性值实际上会传递给JavaScript客户端,为什么这些值会出现在JSON有效负载中并填充类似的内容-如果您将这样的属性添加到发票的客户端元数据中作为未映射的属性,则命名为Breeze实体属性。

Perhaps you're wondering why the unmapped calculated property values are actually communicated to the JavaScript client, why those values appear in the JSON payload and would populate the like-named Breeze entity properties if you add such properties to the client metadata for Invoice as "unmapped properties".

要了解原因,您必须了解使用EF查询的属性和使用Json.NET序列化的属性之间的区别。 EF查询完成后,实例化实体同时具有数据属性(例如ArchivedDate)和计算的属性(已存档)。 [NotMapped]属性不会从Json.NET隐藏属性。 Json.NET序列化实例化对象的所有属性(包括数据和计算的属性),除非您告知不要这样做。例如,您可以使用[Ignore]属性从Json.NET序列化中隐藏Archived属性。

To understand why, you must understand the difference between properties that you query with EF and the properties that you serialize with Json.NET. After the EF query completes, the materialized entities have both the data properties (e.g., ArchivedDate) and the calculated properties (Archived). The [NotMapped] attribute doesn't hide a property from Json.NET. Json.NET serializes ALL properties of the materialized object - both data and calculated properties - unless you tell it not to. For example you could hide the Archived property from Json.NET serialization with the [Ignore] attribute.

toType 是一个红色鲱鱼。而且与这件事无关。

The toType is a red herring and has no bearing on the matter.

这篇关于经过投影,我微风中未映射的属性似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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