breezejs: adding referential constraint to an enti

2020-05-05 15:55发布

问题:

This is a follow-up question to my previous issue - this one was getting a bit messy and is more related to the Telerik Data Service.

The metadata I receive from the server are missing the referential constraints in the association node, although I've set the foreign key attribute on my model.

Therefore I was thinking about manually adding these constraints to my entities in the callback of FetchMetadata.

Is that possible and can someone provide a simple example on how to do it ?

[EDIT]

Here's what I have so far:

  manager.fetchMetadata().then(function () {
        var mandatType = manager.metadataStore.getEntityType("Mandate");
        mandatType.autogeneratedKeyType = breeze.AutoGeneratedKeyType.Identity;

        var openPositionsProp = new breeze.NavigationProperty({
            name: "OpenPositions",
            entityTypeName: "OpenPositions:#DirectDebitModel", 
            isScalar: true,
            associationName: "OpenPosition_Mandate_Mandate_OpenPositions",
            foreignKeyNames: ["Id"]
        });

        mandatType.addProperty(openPositionsProp);
});

But it raises the exception:

The 'Mandate:#DirectDebitModel' EntityType has already been added to a MetadataStore and therefore no additional properties may be added to it.

回答1:

Ok, I have a possible approach that you might be able to use right now.

  1. Fetch the metadata from Teleriks OData feed just like you do now.

  2. Export the metadataStore created as a result of the previous step via the MetadataStore.exportMetadata method. This will return "stringified" json for the same metadata in Breeze's native format. This format is much easier to work with.

  3. Convert this string to json via JSON.parse.

  4. Modify the json to add referential constraint information. See Breeze Native Metadata format docs here

  5. Create a new MetadataStore and import the modified json into it.

  6. Create a new EntityManager with this MetadataStore and use it. This EntityManager should now have complete Breeze metadata for use with the rest of the feed.

    Hope this makes sense!

    We are planning on releasing a form of hybrid metadata in the next release. Unfortunately, it doesn't cover your case because we are focusing on how to add custom metadata to an existing metadataStore, and not actually edit/modify the existing metadata.

    Another alternative is that we (IdeaBlade) do offer consulting for this type of work. We could probably write a tool that does steps 1 thru 6 for you. Please contact breeze@ideablade.com if this is of interest and mention this post.



回答2:

So you are getting meta data but it doesn't have a relationship between the entities. Hmm I have not gotten metaData AND tried to create additional model properties that are related.

Your best bet is to add a property that is a navigation type on the constructor.

http://www.breezejs.com/sites/all/apidocs/classes/EntityType.html#method_addProperty

If it were me, I would try it this way (or something similar) inside of the constructor -

myEntity.addProperty({
                associatedEntity: {
                    entityTypeName: "AssociatedEntity", isScalar: true,
                    associationName: "AssociatedEntity_MyEntitys", foreignKeyNames: ["associatedEntityId"]
                }
            });

Where myEntity is the name of the current entity, AssociatedEntity would be your navigation property, the associatedEntityId is a property of myEntity that refers to the other entity. Of course to have this be a two-way relationship you would need to add a property to AssociatedEntity as well.

associatedEntity.addProperty({
                myEntitys: {
                    entityTypeName: "MyEntity", isScalar: true,
                    associationName: "AssociatedEntity_MyEntitys", foreignKeyNames: ["myEntityId"]
                }
            });