Search code examples
javascriptodatabreeze

breezejs: adding referential constraint to an entity type


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.


Solution

  • 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 [email protected] if this is of interest and mention this post.