Exemple #1
0
        /// <summary>
        /// Returns true if this annotation does not conflict with the given annotation such that
        /// the two can be combined together using the <see cref="M:System.Data.Entity.Infrastructure.Annotations.IndexAnnotation.MergeWith(System.Object)" /> method.
        /// </summary>
        /// <remarks>
        /// Each index annotation contains at most one <see cref="T:System.ComponentModel.DataAnnotations.Schema.IndexAttribute" /> with a given name.
        /// Two annotations are considered compatible if each IndexAttribute with a given name is only
        /// contained in one annotation or the other, or if both annotations contain an IndexAttribute
        /// with the given name.
        /// </remarks>
        /// <param name="other">The annotation to compare.</param>
        /// <returns>A CompatibilityResult indicating whether or not this annotation is compatible with the other.</returns>
        public virtual CompatibilityResult IsCompatibleWith(object other)
        {
            if (object.ReferenceEquals((object)this, other) || other == null)
            {
                return(new CompatibilityResult(true, (string)null));
            }
            IndexAnnotation indexAnnotation = other as IndexAnnotation;

            if (indexAnnotation == null)
            {
                return(new CompatibilityResult(false, Strings.IncompatibleTypes((object)other.GetType().Name, (object)typeof(IndexAnnotation).Name)));
            }
            foreach (IndexAttribute index in (IEnumerable <IndexAttribute>)indexAnnotation._indexes)
            {
                IndexAttribute newIndex = index;
                IndexAttribute me       = this._indexes.SingleOrDefault <IndexAttribute>((Func <IndexAttribute, bool>)(i => i.Name == newIndex.Name));
                if (me != null)
                {
                    CompatibilityResult compatibilityResult = me.IsCompatibleWith(newIndex, false);
                    if (!(bool)compatibilityResult)
                    {
                        return(compatibilityResult);
                    }
                }
            }
            return(new CompatibilityResult(true, (string)null));
        }
Exemple #2
0
 private static void MergeLists(
     ICollection <IndexAttribute> existingIndexes,
     IEnumerable <IndexAttribute> newIndexes,
     PropertyInfo propertyInfo)
 {
     foreach (IndexAttribute newIndex in newIndexes)
     {
         IndexAttribute index = newIndex;
         if (index == null)
         {
             throw new ArgumentNullException("indexAttribute");
         }
         IndexAttribute other = existingIndexes.SingleOrDefault <IndexAttribute>((Func <IndexAttribute, bool>)(i => i.Name == index.Name));
         if (other == null)
         {
             existingIndexes.Add(index);
         }
         else
         {
             CompatibilityResult compatibilityResult = index.IsCompatibleWith(other, false);
             if ((bool)compatibilityResult)
             {
                 existingIndexes.Remove(other);
                 existingIndexes.Add(index.MergeWith(other, false));
             }
             else
             {
                 string str = Environment.NewLine + "\t" + compatibilityResult.ErrorMessage;
                 throw new InvalidOperationException(propertyInfo == (PropertyInfo)null ? Strings.ConflictingIndexAttribute((object)other.Name, (object)str) : Strings.ConflictingIndexAttributesOnProperty((object)propertyInfo.Name, (object)propertyInfo.ReflectedType.Name, (object)other.Name, (object)str));
             }
         }
     }
 }
Exemple #3
0
        internal static IndexAttribute MergeWith(
            this IndexAttribute me,
            IndexAttribute other,
            bool ignoreOrder = false)
        {
            if (object.ReferenceEquals((object)me, (object)other) || other == null)
            {
                return(me);
            }
            CompatibilityResult compatibilityResult = me.IsCompatibleWith(other, ignoreOrder);

            if (!(bool)compatibilityResult)
            {
                throw new InvalidOperationException(Strings.ConflictingIndexAttribute((object)me.Name, (object)(Environment.NewLine + "\t" + compatibilityResult.ErrorMessage)));
            }
            IndexAttribute indexAttribute = me.Name != null ? new IndexAttribute(me.Name) : (other.Name != null ? new IndexAttribute(other.Name) : new IndexAttribute());

            if (!ignoreOrder)
            {
                if (me.Order != -1)
                {
                    indexAttribute.Order = me.Order;
                }
                else if (other.Order != -1)
                {
                    indexAttribute.Order = other.Order;
                }
            }
            if (me.IsClusteredConfigured)
            {
                indexAttribute.IsClustered = me.IsClustered;
            }
            else if (other.IsClusteredConfigured)
            {
                indexAttribute.IsClustered = other.IsClustered;
            }
            if (me.IsUniqueConfigured)
            {
                indexAttribute.IsUnique = me.IsUnique;
            }
            else if (other.IsUniqueConfigured)
            {
                indexAttribute.IsUnique = other.IsUnique;
            }
            return(indexAttribute);
        }