public void UpdateSql(Entity target) { var type = target.LogicalName; var table = type; // + "ExtensionBase"; var fieldsToUpdate = new List <KeyValuePair <string, string> >(); foreach (var field in target.GetFieldsInEntity()) { var fieldType = XrmService.GetFieldType(field, type); var value = target.GetField(field); if (value == null) { if (fieldType == AttributeTypeCode.Money) { fieldsToUpdate.Add(new KeyValuePair <string, string>(field + "_base", "null")); } fieldsToUpdate.Add(new KeyValuePair <string, string>(field, "null")); } else { switch (fieldType) { case AttributeTypeCode.DateTime: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, SqlProvider.ToSqlDateString((DateTime)value))); break; case AttributeTypeCode.Money: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, XrmEntity.GetMoneyValue(value).ToString(CultureInfo.InvariantCulture))); fieldsToUpdate.Add(new KeyValuePair <string, string>(field + "_base", XrmEntity.GetMoneyValue(value).ToString(CultureInfo.InvariantCulture))); break; case AttributeTypeCode.Lookup: var id = XrmEntity.GetLookupGuid(value); if (!id.HasValue) { throw new NullReferenceException("error no id in " + field); } fieldsToUpdate.Add(new KeyValuePair <string, string>(field, WrapSqlString(id.Value.ToString()))); break; case AttributeTypeCode.Picklist: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, XrmEntity.GetOptionSetValue(value).ToString(CultureInfo.InvariantCulture))); break; case AttributeTypeCode.Status: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, XrmEntity.GetOptionSetValue(value).ToString(CultureInfo.InvariantCulture))); break; case AttributeTypeCode.Integer: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, value.ToString())); break; case AttributeTypeCode.Decimal: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, value.ToString())); break; case AttributeTypeCode.Boolean: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, XrmEntity.GetBoolean(value) ? "1" : "0")); break; case AttributeTypeCode.String: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, SqlProvider.ToSqlString((string)value))); break; case AttributeTypeCode.Memo: fieldsToUpdate.Add(new KeyValuePair <string, string>(field, SqlProvider.ToSqlString((string)value))); break; case AttributeTypeCode.Uniqueidentifier: break; default: throw new NotImplementedException("No update logic implemented for field type " + fieldType); } } } var setStrings = string.Join(",", fieldsToUpdate.Select(kv => string.Format("{0} = {1}", kv.Key, kv.Value))); var primaryKey = XrmService.GetPrimaryKeyField(target.LogicalName); var primaryKeySql = SqlProvider.ToSqlString(target.Id); var sql = string.Format("update {0} set {1} where {2} = {3}", table, setStrings, primaryKey, primaryKeySql); SqlProvider.ExecuteNonQuery(sql); }