{"id":273,"date":"2014-06-30T01:36:00","date_gmt":"2014-06-30T01:36:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/odatateam\/2014\/06\/30\/tutorial-sample-refering-when-constructing-edm-model\/"},"modified":"2020-01-07T07:30:08","modified_gmt":"2020-01-07T14:30:08","slug":"tutorial-sample-refering-when-constructing-edm-model","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/odata\/tutorial-sample-refering-when-constructing-edm-model\/","title":{"rendered":"[Tutorial &amp; Sample] Refering when Constructing EDM Model"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>EDM model plays a very important role in OData. When you want to use OData, the first thing should be constructing an EDM model. Previously, you need to build a complete EDM model, which means all types, terms, operations used in an EDM model should be declared in itself. If there are some elements in an existing model (CSDL), which you want to use when constructing a new model. The only method is \u201cre-declared\u201d them in the new model before using. It is costly and unworthy. Now, ODL provides a feature called \u201cModel Reference\u201d. If the new model (referencing model) want to use some types in an existing model (referenced model), you need to add a \u201cReference\u201d of existing model in new model. Then, you can directly using the type defined in existing model without re-declaring it in the new model. With model reference, you can divide a model into many small pieces and reference them in a main model, which is more flexible than before. In this article, we will briefly introduce how to use model reference in OData.<\/p>\n<h2>How to Use Referenced Model to Construct the EDM Model<\/h2>\n<p>There are two methods to get an EDM model. Constructing model in memory by APIs provided on IEdmModel or parsing the existing CSDL by API \u201cEdmxReader.TryParse\u201d. We use the sample example to demo how to consume \u201cModel Reference\u201d in both methods.<\/p>\n<p>Suppose you have a model called \u201cReferencedModel\u201d as following:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/92759b53832a5ee17b16.js\"><\/script><\/p>\n<p>You want to construct a new model (MainModel), which will use some elements in ReferencedModel. The main model is as the following:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/a69de5c362873ab566ac.js\"><\/script><\/p>\n<p>In MainModel, entity type \u201cCustomer\u201d is extended from \u201cPerson\u201d, which is declared in ReferencedModel. The type of property \u201cHomeAddress\u201d in Customer is \u201cAddress\u201d, which is from ReferencedModel, too.<\/p>\n<h3>Constructing EDM Model by API on EdmModel<\/h3>\n<h4>Step 1: Parsing referenced model<\/h4>\n<p>The referenced model may be saved in a FTP server, or shared from a web site. No matter where it is, you need to parse it to IEdmModel before using it. The following code is used to parse \u201cReferencedModel\u201d to IEdmModel, which is saved in local disk:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/43ad3901565ecf9043d7.js\"><\/script><\/p>\n<h4>Step 2: Getting elements from referenced model<\/h4>\n<p>After getting the IEdmModel, you need to find all elements which will be used in the new model. In the example, \u201cPerson\u201d and \u201cAddress\u201d type can be found as following:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/955be8ec4fa8badde961.js\"><\/script><\/p>\n<p>Besides the type, term and operation can also be found via different APIs:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/b754ef8bfdbe1562c3f8.js\"><\/script><\/p>\n<h4>Step 3: Constructing new model<\/h4>\n<p>In the new model, the elements found in referenced model can be directly used to construct the new model. In our example, the following code is used to construct \u201cMainModel\u201d:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/44f1bce60daaa64399e9.js\"><\/script><\/p>\n<p>As there are some types from \u201cReferencedModel\u201d used in \u201cMainModel\u201d. The reference information needs to be added in \u201cMainModel\u201d, including the attribute Namespace and Uri. Please make sure that the Uri should be an accessible address to get the \u201cReferencedModel\u201d. Since client side needs to know the referenced model when parsing the \u201cMainModel\u201d. The following code is to add the reference in \u201cMainModel\u201d.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/92782e98cee5f17b9b07.js\"><\/script><\/p>\n<h3>Constructing EDM Model by Parsing CSDL<\/h3>\n<p>If you do not want to build the model through API on EdmModel, you can also write a CSDL to represent the model, then parse it to IEdmModel. In our example, the following code are used to parse \u201cMainModel\u201d (please refer the CSDL above). The getReferencedSchemaFunc method needs to be provided as a parameter for parsing, to return the XmlReader for referenced model.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/92782e98cee5f17b9b07.js\"><\/script><\/p>\n<h2>How to Consume EDM Model Contains Reference<\/h2>\n<h3>Writing EDM Model<\/h3>\n<p>There is no much difference between writing a normal EDM model and an EDM model contains referenced model. For instance, the following code is to write the \u201cMainModel\u201d:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/d585e00a94b7f4e31c25.js\"><\/script><\/p>\n<h3>Reading EDM Model<\/h3>\n<p>Previously, method \u201cReadMetadataDocument()\u201d is used to read the service metadata and parse it to IEdmModel. But for the model which contains referenced model, \u201cReadMetadataDocument()\u201d is not enough. Referenced models also need to be parsed during the parsing. Thus, \u201cgetReferencedSchemaFunc()\u201d need to be passed to \u201cReadMetadataDocument(Func&lt;Uri, System.Xml.XmlReader&gt; getReferencedModelReaderFunc)\u201d as the parameter, which is used to get referenced model by \u201cUri\u201d of reference. The code are as following:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/98d73aae6a1692ebfb90.js\"><\/script><\/p>\n<h3>Cross-Model Search<\/h3>\n<p>You may need to search the element in IEdmModel. \u201cFindDeclaredXXX\u201d APIs defined in IEdmModel are used to find the elements declared in model, but they do not support search in referenced model. The following APIs are used to do cross-model search:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/df968b4edd24088b0678.js\"><\/script><\/p>\n<h3>CodeGen&amp;T4<\/h3>\n<p>The same as before, only metadata Uri is needed for CodeGen to generate client code for the metadata containing reference element. CodeGen will get the reference model automatically. But, the Uri of reference element should be an http\/https path or file path.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction EDM model plays a very important role in OData. When you want to use OData, the first thing should be constructing an EDM model. Previously, you need to build a complete EDM model, which means all types, terms, operations used in an EDM model should be declared in itself. If there are some elements [&hellip;]<\/p>\n","protected":false},"author":517,"featured_media":3253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odata"],"acf":[],"blog_post_summary":"<p>Introduction EDM model plays a very important role in OData. When you want to use OData, the first thing should be constructing an EDM model. Previously, you need to build a complete EDM model, which means all types, terms, operations used in an EDM model should be declared in itself. If there are some elements [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/users\/517"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media\/3253"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}