Breeze用数据库实体类型管理NODB实体类型 [英] Breeze manage NODB EntityTypes with DB EntityTypes

查看:86
本文介绍了Breeze用数据库实体类型管理NODB实体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Papa的课程CCJS代码来调查Breeze.js和SPA。使用此代码,我试图管理来自服务器的附加信息,但不是来自EntityFramework的元数据中包含的实体。

i´m using the Papa's course CCJS code to investigate Breeze.js and SPA. Using this code i´m trying to manage aditional information that cames from server but that is not an Entity contained in the Metadata that cames from EntityFramework.

所以我创建了一个NO -DB类称为Esto和类似Lookups的Server方法:

So i created a NO-DB class called Esto and a Server method like Lookups:

  [HttpGet]
  public object Informacion()
    {
       var a = new Esto(....);
       var b = new Esto(.....);
       var c = new Esto(......);

    return new {a,b,c};
    }

然后在configure.etadataStore中的model.js中调用:

then in model.js inside configureMetadataStore i call:

metadataStore.addEntityType({
    shortName: "Esto",
    namespace:"CodeCamper",
    dataProperties:{
      id: {dataType: breeze.DataType.Int32,isPartOfKey: true},
      name: {dataType: breeze.DataType.String}
      }
   };

,并在模型中定义EntityNames数组:esto:'Esto'为实体

and also define in the model entityNames array: esto:'Esto' as an Entity

现在在context.js中加载此代码,以创建服务器端方法,如getLookups,但称为getInformacion:

now in the context.js i load this creating a server side method like getLookups but called getInformacion:

    function getInformacion(){
            return EntityQuery.from('Informacion')
                   .using(manager).execute()
     }

,然后在成功方法中的primeData中调用:

and then inside primeData in the success method call this:

datacontext.informacion = {
    esto: getLocal('Esto',nombre)};

其中getLocal是:

where getLocal is:

  function getLocal(resource, ordering)
   {
        var query = EntityQuery.from(resource).orderBy(ordering);
        return manager.executeQueryLocally(query);
   }

我在getLocal包含的查询中遇到错误,指出不能

I get an error in the query contained in the getLocal that states that Can not find EntityType for either entityTypeName: 'undefined' or resourceName:'Esto'.

我做错了什么?

谢谢

推荐答案

您快到了! :-)如果您在查询中指定目标 EntityType ,我认为它会起作用。

You were almost there! :-) Had you specified the target EntityType in the query I think it would have worked.

尝试一下:


var query = EntityQuery.from(resource).orderBy(ordering).toType('Esto');

toType()方法告诉Breeze所返回的顶级对象此查询的类型为 Esto

The toType() method tells Breeze that the top-level objects returned by this query will be of type Esto.

让我们考虑Breeze如何解释查询规范。

Let's think about how Breeze interprets a query specification.

请注意,您像平常一样通过命名资源开始查询>将提供数据。该资源通常是到远程服务端点的路径段,可能是Web API控制器方法的名称,也就是名为 Foos的方法。

Notice that you began your query, as we usually do, by naming the resource which will supply the data. This resource is typically a path segment to a remote service endpoint, perhaps the name of a Web API controller method ... a method named "Foos".

必须理解查询资源名称很少与 EntityType 名称相同!它们可能相似- Foos(复数)类似于类型名称 Foo(单数)。但是资源名称可以是其他名称。可能是 GetFoos或 GreatFoos或其他任何东西。重要的是服务方法返回 Foo实体。

It's critical to understand that the query resource name is rarely the same as the EntityType name! They may be similar - "Foos" (plural) is similar to the type name "Foo" (singular). But the resource name could be something else. It could be "GetFoos" or "GreatFoos" or anything at all. What matters is that the service method returns "Foo" entities.

Breeze需要一种将资源名称与 EntityType 名称。 Breeze自己不知道相关性。 toType()方法是一种告知Breeze的方法。

Breeze needs a way to correlate the resource name with the EntityType name. Breeze doesn't know the correlation on its own. The toType() method is one way to tell Breeze about it.

您通常不添加 toType()查询。为什么是现在?

You generally don't add toType() to your queries. Why now?

大多数时候[1],Breeze不需要知道 EntityType 直到数据从服务器到达 。当JSON查询结果包含类型名称时(例如,当它们来自Breeze Web API控制器时),Breeze可以在没有我们帮助的情况下将到达的JSON数据映射到实体中……假设这些类型名称在元数据中。

Most of the time [1], Breeze doesn't need to know the EntityType until after the data arrive from the server. When the JSON query results includes the type name (as they do when they come from a Breeze Web API controller for example), Breeze can map the arriving JSON data into entities without our help ... assuming that these type names are in metadata.

查询缓存时...用 executeQueryLocally ... Breeze必须先知道要搜索 的缓存实体集,然后才能在本地查询

When you query the cache ... say with executeQueryLocally ... Breeze must know which cached entity-set to search before it can query locally.

如果您使用 toType()指定类型,它将知道。但是,如果省略 toType(),Breeze必须使用查询的资源名称。

It "knows" if you specify the type with toType(). But if you omit toType(), Breeze has to make do with the query's resource name.

Breeze不会猜吧。相反,它将在EntityType / ResourceName映射中查找与查询资源名称匹配的实体集。

Breeze doesn't guess. Instead, it looks in an EntityType/ResourceName map for the entity-set that matches the query resource name.

资源名称引用服务端点,而不是缓存的实体集。例如,没有名为 Informacion的实体集。因此,Breeze使用 EntityType / ResourceName 映射来查找与查询资源名称关联的实体类型。

The resource name refers to a service endpoint, not a cached entity-set. There is no entity-set named "Informacion", for example. So Breeze uses an EntityType/ResourceName map to find the entity type associated with the query resource name.

EntityType / ResourceName 映射是Breeze MetadataStore 中的一项。您可能从未听说过。非常好;除非您做了一些不寻常的事情,例如定义自己的类型,否则您不必考虑它。

The EntityType/ResourceName map is one of the items in the Breeze MetadataStore. You've probably never heard of it. That's good; you shouldn't have to think about it ... unless you do something unusual like define your own types.

新的 MetadataStore 的映射开始为空。如果那些元数据包含EntityType /资源映射,则Breeze从服务器元数据填充它。

The map of a new MetadataStore starts empty. Breeze populates it from server metadata if those metadata contain EntityType/Resource mappings.

例如,Breeze EFContextProvider 生成元数据,并从 DbSet 名称。当您定义 Foo 类并将其从 DbContext 公开为 DbSet 名为 Foos, EFContextProvider 元数据生成器将映射从 Foos资源名称映射到 Foo 实体类型。

For example, the Breeze EFContextProvider generates metadata with mappings derived from DbSet names. When you define a Foo class and exposed it from a DbContext as a DbSet named "Foos", the EFContextProvider metadata generator adds a mapping from the "Foos" resource name to the Foo entity type.

控制器开发人员倾向于使用 DbSet 名称作为方法名称。常规的Breeze Web API控制器 Foo查询方法如下所示:

Controller developers tend to use DbSet names for method names. The conventional Breeze Web API controller "Foo" query method looks like this:


[Get]
public IQueryable<Foo> Foos() {...}

现在,如果您进行如下查询:

Now if you take a query such as this:


var query = EntityQuery.from('Foos').where(...);

并将其应用于缓存


manager.query.executeLocally(query).then(...);

它只是有效。

为什么?因为


  • Foos是服务器上 DbSet 的名称

  • EFContextProvider 生成的元数据映射[ Foos到 Model.Foo ]

  • Web API控制器提供了 Foos 操作方法。

  • BreezeJS query 指定 Foos

  • executeLocally 方法找到[[Foos] -to-元数据中的 Model.Foo ]映射,并将查询应用于 Foo 的实体集。

  • "Foos" is the name of a DbSet on the server
  • The EFContextProvider generated metadata mapping ["Foos" to Model.Foo]
  • The Web API Controller offers a Foos action method.
  • The BreezeJS query specifies "Foos"
  • The executeLocally method finds the ["Foos"-to-Model.Foo] mapping in metadata and applies the query to the entity-set for Foo.

端到端约定对您无声工作。

The end-to-end conventions work silently in your favor.

...直到您提到的资源名称不在EntityType / ResourceName映射中!

... until you mention a resource name that is not in the EntityType/ResourceName map!

没问题!

您可以如下添加自己的资源到实体类型的映射:

You can add your own resource-to-entity-type mappings as follows:


var metadataStore = manager.metadataStore;
var typeName = 'some-type-name';
var entityType = metadataStore.getEntityType(typeName);

metadataStore.setEntityTypeForResourceName(resource, entityType);

微风对类型名称也很满意:

Breeze is also happy with just the name of the type:


metadataStore.setEntityTypeForResourceName(resource, typeName);

在您的情况下,您的 DataContext 顶部附近:

In your case, somewhere near the top of your DataContext, you could write:


var metadataStore = manager.metadataStore;
// map two resource names to Esto
metadataStore.setEntityTypeForResourceName('Esto', 'Esto'); 
metadataStore.setEntityTypeForResourceName('Informacion', 'Esto');



不要过度使用 toType()



toType()方法是一个很好的捷径解决方案,当您需要将查询结果中的顶级对象映射到 EntityType 。您不必费心注册资源名称。

Don't over-use toType()

The toType() method is a good short-cut solution when you need to map the top-level objects in the query result to an EntityType. You don't have to mess around with registering resource names.

但是,您必须记住将 toType()添加到每个需要它的查询中。使用资源到实体类型的映射配置Breeze元数据,您每次都会得到想要的行为。

However, you must remember to add toType() to every query that needs it. Configure Breeze metadata with the resource-to-entity-type mapping and you'll get the desired behavior every time.

[1] 大多数时候,Breeze不需要知道 EntityType ,直到数据从服务器到达为止。一个重要的例外-在此讨论范围之外-是查询过滤器涉及日期/时间。

[1] "Most of the time, Breeze doesn't need to know the EntityType until after the data arrive from the server." One important exception - out of scope for this discussion - is when the query filter involves a Date/Time.

这篇关于Breeze用数据库实体类型管理NODB实体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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