質問

i'm trying to implement mock dataservice. I'm registering new data type, creating a new entity with init data, but on executing the query i'm getting an error:"There is no metadata available for this query". I can't figure out why it happends. My entity manager contains the datatype i had registered, also it contains a collection of created entities. I thought my query fails because of namespace so i tried to query "Tag:#Football.Models" - the same result. Also i had tried executeLocally() method, same stuff...

Steps:

1) Create breeze dataservice, datastore and manager.

2) Create new metadata type and add it to datastore.

3) Create new entity of registered type and init it by some mock data.

4) Execute query to get collection of entities; Fail at step 4 - an error occurs: "Error: There is no metadata available for this query"

Here is my code:

          //1st step
           var mockDataService = new breeze.DataService({
                serviceName: "mockDataService",
                hasServerMetadata: false
            });

            var mockMetadataStore = new breeze.MetadataStore(
                {
                    namingConvention: breeze.NamingConvention.camelCase
                });

            var queryOptions = new breeze.QueryOptions({
                fetchStrategy: breeze.FetchStrategy.FromLocalCache
            });

            var entityManager = new breeze.EntityManager({
                dataService: mockDataService,
                metadataStore: mockMetadataStore,
                queryOptions: queryOptions
            });
           // 2nd step
            var et = new breeze.EntityType({
                shortName: "Tag",
                namespace: "Football.Models",
                autoGeneratedKeyType: breeze.AutoGeneratedKeyType.Identity,
                defaultResourceName: "tags"
            });
            et.addProperty(new breeze.DataProperty({
                name: "id",
                dataType: breeze.DataType.Int32,
                isNullable: false,
                isPartOfKey: true
            }));
            et.addProperty(new breeze.DataProperty({
                name: "name",
                dataType: breeze.DataType.String,
                isNullable: false
            }));
            mockMetadataStore.addEntityType(et);
            mockMetadataStore.registerEntityTypeCtor("Tag", null);
                //3rd step 
                etType = mockMetadataStore.getEntityType("Tag");
            var newTag = etType.createEntity({id:1,name:"tag"});
            entityManager.addEntity(newTag);
                // 4th step
                var a =  breeze.EntityQuery
              .from("Tag")
              .using(entityManager).execute()
              .then(querySucceed).fail( function(err) {
                    alert(err);
                                // and i got an error:
                                // Error: There is no metadata available for this query
               });;
役に立ちましたか?

解決

Update as of version 1.2.7

You should no longer need to call the setEntityTypeForResourceName or addDataService methods in order to accomplish the mocking setup you described.

--- Previous answer ----------------------------------------

You are very close. All you need to do is add the following two lines

   mockMetadataStore.setEntityTypeForResourceName("Tag", et); // or "Tags"
   mockMetadataStore.addDataService(mockDataService);

after this line

   mockMetadataStore.addEntityType(et);

Neither of these should actually be required in this case, but for now they are. The need for this "extra" code will be removed in the next release. These methods are still useful but just not in this specific case.

Also, you should change either your defaultResourceName or your EntityQuery.from clause so that the names match. (This includes case - see comment below).

The reason is that the defaultResourceName is the name of the "collection/resource" that you are querying, not the name of the "entityType", i.e. the resource names are what you are passing into the EntityQuery.from clause. A good convention is that the resource name is the pluralized version of the entityType name that it queries, although this is by no means required. So you can either change either the "from" clause or the "defaultResourceName"

Side note, multiple resourceNames can return the same entityType, hence the "default" in the name "defaultResourceName".

In addition, only property names go thru namingConventions' conversion, so your resourceNames should be exactly what the server expects them to be.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top