示例#1
0
        private static AttributeCollection GetAttributes(CrmEntityBase crmEntity, PropertyHelper[] helpers)
        {
            var attributes      = new AttributeCollection();
            var relevantHelpers = helpers.Where(ph => ph.CRMFieldBaseAttribute != null);

            foreach (var helper in relevantHelpers)
            {
                var key        = helper.CRMFieldBaseAttribute.AttributeName;
                var modelValue = helper.GetValue(crmEntity);
                var value      = modelValue;

                if (helper.CRMFieldBaseAttribute.Type != CRMFieldType.Basic)
                {
                    value = GetCrmValue(helper.CRMFieldBaseAttribute, modelValue);
                }

                if (!ignoreNulls)
                {
                    attributes.Add(key, value);
                }
                else if (value != null)
                {
                    attributes.Add(key, value);
                }
            }
            return(attributes);
        }
示例#2
0
        public static Entity ToEntity(CrmEntityBase model)
        {
            var entity     = new Entity(model.LogicalName);
            var helpers    = PropertyHelper.GetProperties(model.GetType());
            var attributes = GetAttributes(model, helpers);

            if (attributes.Contains($"{model.LogicalName}id"))
            {
                var id = (Guid)attributes[$"{model.LogicalName}id"];
                if (id == default)
                {
                    attributes.Remove($"{model.LogicalName}id");
                }
                else
                {
                    entity.Id = id;
                }
            }
            entity.Attributes.AddRange(attributes);
            return(entity);
        }