/// <summary> /// Serializes the given <see cref="T:System.Data.Entity.Infrastructure.Annotations.IndexAnnotation" /> into a string for storage in the EDMX XML. /// </summary> /// <param name="name">The name of the annotation that is being serialized.</param> /// <param name="value">The value to serialize which must be an IndexAnnotation object.</param> /// <returns>The serialized value.</returns> public virtual string Serialize(string name, object value) { Check.NotEmpty(name, nameof(name)); Check.NotNull <object>(value, nameof(value)); IndexAnnotation indexAnnotation = value as IndexAnnotation; if (indexAnnotation == null) { throw new ArgumentException(Strings.AnnotationSerializeWrongType((object)value.GetType().Name, (object)typeof(IndexAnnotationSerializer).Name, (object)typeof(IndexAnnotation).Name)); } StringBuilder stringBuilder = new StringBuilder(); foreach (IndexAttribute index in indexAnnotation.Indexes) { stringBuilder.Append(IndexAnnotationSerializer.SerializeIndexAttribute(index)); } return(stringBuilder.ToString()); }
/// <summary> /// Deserializes the given string back into an <see cref="T:System.Data.Entity.Infrastructure.Annotations.IndexAnnotation" /> object. /// </summary> /// <param name="name">The name of the annotation that is being deserialized.</param> /// <param name="value">The string to deserialize.</param> /// <returns>The deserialized annotation value.</returns> /// <exception cref="T:System.FormatException">If there is an error reading the serialized value.</exception> public virtual object Deserialize(string name, string value) { Check.NotEmpty(name, nameof(name)); Check.NotEmpty(value, nameof(value)); value = value.Trim(); if (!value.StartsWith("{", StringComparison.Ordinal) || !value.EndsWith("}", StringComparison.Ordinal)) { throw IndexAnnotationSerializer.BuildFormatException(value); } List <IndexAttribute> indexAttributeList = new List <IndexAttribute>(); List <string> list = ((IEnumerable <string>)IndexAnnotationSerializer._indexesSplitter.Split(value)).Select <string, string>((Func <string, string>)(s => s.Trim())).ToList <string>(); list[0] = list[0].Substring(1); int index = list.Count - 1; list[index] = list[index].Substring(0, list[index].Length - 1); foreach (string input in list) { IndexAttribute indexAttribute = new IndexAttribute(); if (!string.IsNullOrWhiteSpace(input)) { foreach (string str1 in ((IEnumerable <string>)IndexAnnotationSerializer._indexPartsSplitter.Split(input)).Select <string, string>((Func <string, string>)(s => s.Trim()))) { if (str1.StartsWith("Name:", StringComparison.Ordinal)) { string str2 = str1.Substring(5).Trim(); if (string.IsNullOrWhiteSpace(str2) || !string.IsNullOrWhiteSpace(indexAttribute.Name)) { throw IndexAnnotationSerializer.BuildFormatException(value); } indexAttribute.Name = str2.Replace("\\,", ",").Replace("\\{", "{"); } else if (str1.StartsWith("Order:", StringComparison.Ordinal)) { int result; if (!int.TryParse(str1.Substring(6).Trim(), out result) || indexAttribute.Order != -1) { throw IndexAnnotationSerializer.BuildFormatException(value); } indexAttribute.Order = result; } else if (str1.StartsWith("IsClustered:", StringComparison.Ordinal)) { bool result; if (!bool.TryParse(str1.Substring(12).Trim(), out result) || indexAttribute.IsClusteredConfigured) { throw IndexAnnotationSerializer.BuildFormatException(value); } indexAttribute.IsClustered = result; } else { if (!str1.StartsWith("IsUnique:", StringComparison.Ordinal)) { throw IndexAnnotationSerializer.BuildFormatException(value); } bool result; if (!bool.TryParse(str1.Substring(9).Trim(), out result) || indexAttribute.IsUniqueConfigured) { throw IndexAnnotationSerializer.BuildFormatException(value); } indexAttribute.IsUnique = result; } } } indexAttributeList.Add(indexAttribute); } return((object)new IndexAnnotation((IEnumerable <IndexAttribute>)indexAttributeList)); }