示例#1
0
        public void CreateRequiredRecords()
        {
            var orgFactory = new OrganizationServiceFactory();
            var orgSrv     = orgFactory.GetNewOrganizationService();
            var context    = new XrmServiceContext(orgSrv);

            var accFac = new AccountFactory();

            context.AddObject(accFac.CurrentEntity);

            var conFac = new ContactFactory();

            context.AddRelatedObject(accFac.CurrentEntity, new Relationship("contact_customer_accounts"), conFac.CurrentEntity);

            context.SaveChanges();

            var incFac = new IncidentFactory();

            incFac.CurrentEntity.CustomerId = accFac.CurrentEntity.ToEntityReference();
            context.AddObject(incFac.CurrentEntity);
            context.Attach(accFac.CurrentEntity);

            var wpFac = new WorkPackageFactory();

            context.AddRelatedObject(accFac.CurrentEntity, new Relationship("a24_account_workpackage_customer_ref"), wpFac.CurrentEntity);
            wpFac.CurrentEntity.a24_customer_ref = accFac.CurrentEntity.ToEntityReference();

            context.SaveChanges();
        }
        public void When_calling_context_add_addrelated_and_save_changes_entities_are_added_to_the_faked_context()
        {
            var context = new XrmFakedContext();

            var relationship = new XrmFakedRelationship()
            {
                IntersectEntity    = "accountleads",
                Entity1Attribute   = "accountid",
                Entity2Attribute   = "leadid",
                Entity1LogicalName = "account",
                Entity2LogicalName = "lead"
            };

            context.AddRelationship("accountleads", relationship);

            var service = context.GetFakedOrganizationService();

            using (var ctx = new XrmServiceContext(service))
            {
                var account = new Account()
                {
                    Name = "Test account"
                };
                ctx.AddObject(account);

                var contact = new Lead()
                {
                    FirstName = "Jane", LastName = "Doe"
                };
                ctx.AddRelatedObject(account, new Relationship("accountleads"), contact);
                var result = ctx.SaveChanges();

                var resultaccount = ctx.CreateQuery <Account>()
                                    .ToList()
                                    .FirstOrDefault();

                Assert.NotNull(resultaccount);
                Assert.Equal("Test account", resultaccount.Name);

                var reaultlead = ctx.CreateQuery <Lead>()
                                 .ToList()
                                 .FirstOrDefault();

                Assert.NotNull(reaultlead);
                Assert.Equal("Jane", reaultlead.FirstName);
                Assert.Equal("Doe", reaultlead.LastName);

                var relationshipRecords = ctx.CreateQuery("accountleads")
                                          .ToList();
                Assert.NotEmpty(relationshipRecords);
            }
        }
        public void When_calling_context_add_addrelated_and_save_changes_entities_are_added_to_the_faked_context()
        {
            var context = new XrmFakedContext();

            var relationship = new XrmFakedRelationship()
            {
                IntersectEntity = "accountleads",
                Entity1Attribute = "accountid",
                Entity2Attribute = "leadid",
                Entity1LogicalName = "account",
                Entity2LogicalName = "lead"
            };
            context.AddRelationship("accountleads", relationship);

            var service = context.GetFakedOrganizationService();

            using (var ctx = new XrmServiceContext(service))
            {
                var account = new Account() { Name = "Test account" };
                ctx.AddObject(account);

                var contact = new Lead() { FirstName = "Jane", LastName = "Doe" };
                ctx.AddRelatedObject(account, new Relationship("accountleads"), contact);
                var result = ctx.SaveChanges();

                var resultaccount = ctx.CreateQuery<Account>()
                                       .ToList()
                                       .FirstOrDefault();

                Assert.NotNull(resultaccount);
                Assert.Equal("Test account", resultaccount.Name);

                var reaultlead = ctx.CreateQuery<Lead>()
                                    .ToList()
                                    .FirstOrDefault();

                Assert.NotNull(reaultlead);
                Assert.Equal("Jane", reaultlead.FirstName);
                Assert.Equal("Doe", reaultlead.LastName);

                var relationshipRecords = ctx.CreateQuery("accountleads")
                                             .ToList();
                Assert.NotEmpty(relationshipRecords);
            }
        }