コード例 #1
0
        public void IsCompatibleWith_returns_true_if_other_is_same_or_null()
        {
            var attribute = new IndexAttribute();

            Assert.True(attribute.IsCompatibleWith(attribute));
            Assert.True(attribute.IsCompatibleWith(null));
        }
コード例 #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));
             }
         }
     }
 }
コード例 #3
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));
        }
コード例 #4
0
        internal static IndexAttribute MergeWith(this IndexAttribute me, IndexAttribute other, bool ignoreOrder = false)
        {
            DebugCheck.NotNull(me);

            if (ReferenceEquals(me, other) ||
                other == null)
            {
                return(me);
            }

            var isCompatible = me.IsCompatibleWith(other, ignoreOrder);

            if (!isCompatible)
            {
                throw new InvalidOperationException(
                          Strings.ConflictingIndexAttribute(me.Name, Environment.NewLine + "\t" + isCompatible.ErrorMessage));
            }

            var merged = me.Name != null
                ? new IndexAttribute(me.Name)
                : other.Name != null ? new IndexAttribute(other.Name) : new IndexAttribute();

            if (!ignoreOrder)
            {
                if (me.Order != -1)
                {
                    merged.Order = me.Order;
                }
                else if (other.Order != -1)
                {
                    merged.Order = other.Order;
                }
            }

            if (me.IsClusteredConfigured)
            {
                merged.IsClustered = me.IsClustered;
            }
            else if (other.IsClusteredConfigured)
            {
                merged.IsClustered = other.IsClustered;
            }

            if (me.IsUniqueConfigured)
            {
                merged.IsUnique = me.IsUnique;
            }
            else if (other.IsUniqueConfigured)
            {
                merged.IsUnique = other.IsUnique;
            }

            return(merged);
        }
コード例 #5
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);
        }
コード例 #6
0
        public void Add(string columnName, IndexAttribute index)
        {
            DebugCheck.NotEmpty(columnName);
            DebugCheck.NotNull(index);

            Debug.Assert(_index.Name == index.Name);

            if (_columns.ContainsKey(index.Order))
            {
                throw new InvalidOperationException(
                          Strings.OrderConflictWhenConsolidating(index.Name, _table, index.Order, _columns[index.Order], columnName));
            }

            _columns[index.Order] = columnName;

            var compat = _index.IsCompatibleWith(index, ignoreOrder: true);

            if (!compat)
            {
                throw new InvalidOperationException(Strings.ConflictWhenConsolidating(index.Name, _table, compat.ErrorMessage));
            }

            _index = _index.MergeWith(index, ignoreOrder: true);
        }