コード例 #1
0
        public void When_updating_an_entity_by_alternate_key_the_context_should_reflect_changes()
        {
            var context = new XrmFakedContext();
            var service = context.GetOrganizationService();

            var accountMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityMetadata();

            accountMetadata.LogicalName = Account.EntityLogicalName;
            var alternateKeyMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata();

            alternateKeyMetadata.KeyAttributes = new string[] { "AccountNumber" };
            accountMetadata.SetFieldValue("_keys", new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata[]
            {
                alternateKeyMetadata
            });
            context.InitializeMetadata(accountMetadata);

            var e = new Entity("account");

            e["AccountNumber"] = 9000;
            e["name"]          = "Before update";
            var guid = service.Create(e);

            Assert.Equal(context.Data["account"][guid]["name"], "Before update");

            //now update the name
            e         = new Entity("account", "AccountNumber", 9000);
            e["name"] = "After update";
            service.Update(e);

            Assert.Equal(context.Data["account"][guid]["name"], "After update");
        }
コード例 #2
0
        public static void Should_Retrieve_A_Correct_Entity_By_Alternate_Key()
        {
            var fakedContext    = new XrmFakedContext();
            var accountMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityMetadata();

            accountMetadata.LogicalName = Account.EntityLogicalName;
            var alternateKeyMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata();

            alternateKeyMetadata.KeyAttributes = new string[] { "alternateKey" };
            accountMetadata.SetFieldValue("_keys", new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata[]
            {
                alternateKeyMetadata
            });
            fakedContext.InitializeMetadata(accountMetadata);
            var account = new Entity(Account.EntityLogicalName);

            account.Id = Guid.NewGuid();
            account.Attributes.Add("alternateKey", "key");
            fakedContext.Initialize(account);
            var fakedService = fakedContext.GetOrganizationService();

            var request = new RetrieveRequest
            {
                Target    = new EntityReference(Account.EntityLogicalName, "alternateKey", "key"),
                ColumnSet = new ColumnSet(allColumns: true)
            };

            var retrievedAccount = (RetrieveResponse)fakedService.Execute(request);

            Assert.Equal(account.Id, retrievedAccount.Entity.Id);
        }