THE SQL Server Blog Spot on the Web

Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | |
in Search

Master Data Services Team

Blog for the Microsoft SQL Server Master Data Services (MDS) team. Blog posts are contributed by various team members.

Creating Entities using the MDS WCF API

(this post was contributed by Brent McBride, Senior Software Engineer on the MDS team).

The below example shows how to create a new model and add new entities in the same call to the MetadataCreate API operation:

 

            Example 1: Add a model and its entities in the same operation.

 

            // Add a new model.

            string modelName = "My New Model Name";

            Metadata metaData = new Metadata();

            metaData.Models = new List<Model> {

                                new Model() {

                                    Identifier = new Identifier() { Name = modelName } } };

 

            // Add new entities to the new model.

            foreach (string entityName in entityNames)

            {

                Entity entity = new Entity();

                entity.Identifier = new ModelContextIdentifier();

                entity.Identifier.Name = entityName;

                 

                metaData.Models[0].Entities.Add(entity);

            }

 

            OperationResult operationResult = new OperationResult();

            client.MetadataCreate(international, metaData, true, out operationResult);

The Metadata DataContract contains several properties that are lists of metadata objects, such as Models, Entities, Versions, Attributes, etc. However, in any single call to a metadata CRUD operation (including MetadataCreate), only one of these properties may be non-empty. Otherwise, the operation returns error 200000 “The operation permits only one metadata property list.” Thus, Example 2 is invalid:

           

            Example 2: Incorrectly add model and entities.

 

            // Add a new model.

            string modelName = "My New Model Name";

            Metadata metaData = new Metadata();

            metaData.Models = new List<Model> {

                                new Model() {

                                    Identifier = new Identifier() { Name = modelName } } };

 

            // Add new entities to the new model.

            metaData.Entities = new List<Entity>();

            foreach (string entityName in entityNames)

            {

                Entity entity = new Entity();

                entity.Identifier = new ModelContextIdentifier();

                entity.Identifier.Name = entityName;

 

                metaData.Entities.Add(entity); // Error! Both Models and Entities cannot be populated.

            }

 

            OperationResult operationResult = new OperationResult();

            client.MetadataCreate(international, metaData, true, out operationResult); 

            // Result: Error 200000

The new entities must be added directly underneath the new model to which they belong, as per Example 1. However, the Metadata.Entities property comes in handy when adding entities to an already existing model, as Example 3 shows:

           

            Example 3: Add entities to a preexisting model.

 

            // Reference an existing model.

            string modelName = "My Existing Model Name";

            Metadata metaData = new Metadata();

 

            // Add new entities to the existing model.

            metaData.Entities = new List<Entity>();

            foreach (string entityName in entityNames)

            {

                Entity entity = new Entity();

                entity.Identifier = new ModelContextIdentifier();

                entity.Identifier.Name = entityName;

                entity.Identifier.ModelId = new Identifier { Name = modelName };

 

                metaData.Entities.Add(entity); // No error this time because the Models property is empty.

            }

 

            OperationResult operationResult = new OperationResult();

            client.MetadataCreate(international, metaData, true, out operationResult); 

As shown in the highlighted line of Example 3, each entity must reference the model to which it pertains. Without this context, the operation returns error 120003 “The user does not have permission or the object ID not valid”. This error message is intentionally ambiguous for the sake of security. Using the same error code for both insufficient permissions and invalid IDs makes it harder for an attacker to glean information about the attack surface. Although Example 3 only deals with creating entities, the same principle could also apply to other API operations and metadata objects such as attributes. Error 120003 could occur if there is not enough contextual information provided for the operation to uniquely identify a referenced metadata object.

 

© 2010 Microsoft Corporation.  All rights reserved.  This information is provided “as-is”. Information and views expressed, including URL and other Internet Web site references, may change without notice. You bear the risk of using this information. Examples are provided for illustration only.  Provision of this information does not provide you with any legal rights to any intellectual property in any Microsoft product. You may copy and use this information for your internal, reference purposes only.

Published Friday, January 29, 2010 1:03 PM by mattande
Filed under: ,

Comments

 

Diego E Maciado said:

How We can Add data to the stage area?

Can you send us an c sharp example?

We need to create a web page who permit add Member to and specific Entity?

Thansks

June 11, 2010 4:02 PM
 

mattande said:

One can use the EntityMembersMerge Service Operation to create/update members synchronously without using staging.  One can also use the ModelMembersBulkMerge Service Operation to create/update members asynchronously using staging if using staging is a requirement.  One can also write directly to the staging tables without using the Web Service API if one really needs to use staging.

July 27, 2010 2:20 PM
 

Jacob said:

I was trying to create A Model and Entity.But MetaDataCreate accepts only one argument (MetadataCreateRequest Instance).So I was not able to do thsis line

client.MetadataCreate(international, metaData, true, out operationResult);

So I tried this code..

   MetadataCreateRequest request = new MetadataCreateRequest();

               EntityMembersCreateRequest emRequest = new EntityMembersCreateRequest();

             request.Metadata = new Metadata();

               MetadataCreateResponse response = new MetadataCreateResponse();

              EntityMembers em = new EntityMembers();

               Identifier iden = new Identifier();

               iden.Name = "Jen";

               iden.InternalId = 11;

               International inter = new International();

               inter.Locale = "Eng";

               inter.Tz = "tz";

               Console.WriteLine("GetType:"+inter.GetType());

               //  em.EntityId = iden;

               // em.ModelId = iden;

               //em.VersionId = iden;

               request.Metadata.Models = new List<Model>() { new Model() { Identifier = new Identifier() { Name = "TestCustomerModel" } } };

               request.International = inter;

               Entity entity = new Entity();

               entity.Identifier = new ModelContextIdentifier();

               entity.Identifier.Name = "JEntity";

               entity.Identifier.InternalId = 10;

               entity.StagingName = "JStagingName";

               entity.IsBase = true;

              // entity.MemberTypes=new En

              // entity.GetType = MdsServiceReference1.Entity;

            //   entity.ToString = "JacEn";

               entity.Identifier.ModelId = new Identifier { Name = "TestCustomerModel" };

               request.Metadata.Entities = new List<Entity>();

               //request.Metadata.Entities.

               request.Metadata.Entities.Add(entity);

               //request.Metadata.Entities = entity;

              response = mdsProxy.MetadataCreate(request);

              Console.WriteLine("End");

Can u help me in this issue..

January 27, 2013 8:52 AM
 

Jacob said:

Can u Send me C sharp sample code for creating a sample model , then a sample entity inside with one or two attributes and the populating  this entity with sample members.

January 27, 2013 9:00 AM
 

Jac86 said:

Hi

I was trying to add members to already existing model with model name=CustomerModel ,Entity Name=tCustomer ,verson=VERSION_1)..But getting failed in first line itself while creating entity member request..Not going to next line.Can any one help me..

Here is my C sharp code..

EntityMembersCreateRequest emRequest = new EntityMembersCreateRequest();

               emRequest.Members.ModelId = new Identifier { Name = "CustomerModel" };

               Console.WriteLine("Test:");

                   emRequest.Members.EntityId = new Identifier { Name = "tCustomer" };

                 emRequest.Members.VersionId = new Identifier { Name = "VERSION_1" };

                emRequest.Members.MemberType = MemberType.Leaf;

                EntityMembers em = new EntityMembers();

                 em.MemberType = MemberType.Leaf;

                    em.Members = new List<Member>();

                 Member m = new Member();

                  m.MemberId = new MemberIdentifier();

                m.MemberId.Name = "test";

                m.MemberId.Code = "test";

                   em.Members.Add(m);

               emRequest.Members.Members.Add(m);

              mdsProxy.EntityMembersCreate(emRequest);

              Console.WriteLine("End");

January 27, 2013 9:30 AM
 

;; said:

;;

March 13, 2013 2:16 AM
New Comments to this post are disabled
Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement