Esempio n. 1
0
        /// <summary>
        ///     Adds a new alternate key to this entity type.
        /// </summary>
        /// <param name="entityType"> The entity type to add the alternate key to. </param>
        /// <param name="property"> The property to use as an alternate key. </param>
        /// <returns> The newly created key. </returns>
        public static IMutableKey AddKey(
            [NotNull] this IMutableEntityType entityType, [NotNull] IMutableProperty property)
        {
            Check.NotNull(entityType, nameof(entityType));

            return(entityType.AddKey(new[] { property }));
        }
Esempio n. 2
0
        /// <summary>
        ///     Gets the existing alternate key defined on a set of properties, or creates a new one if one is not
        ///     already defined.
        /// </summary>
        /// <param name="entityType"> The entity type to get or create the alternate key on. </param>
        /// <param name="properties"> The properties that are used as the alternate key. </param>
        /// <returns> The existing or newly created alternate key. </returns>
        public static IMutableKey GetOrAddKey(
            [NotNull] this IMutableEntityType entityType, [NotNull] IReadOnlyList <IMutableProperty> properties)
        {
            Check.NotNull(entityType, nameof(entityType));

            return(entityType.FindKey(properties) ?? entityType.AddKey(properties));
        }
Esempio n. 3
0
        private static void CloneKeys(IReadOnlyEntityType sourceEntityType, IMutableEntityType targetEntityType)
        {
            foreach (var key in sourceEntityType.GetDeclaredKeys())
            {
                var clonedKey = targetEntityType.AddKey(
                    key.Properties.Select(p => targetEntityType.FindProperty(p.Name)).ToList());
                if (key.IsPrimaryKey())
                {
                    targetEntityType.SetPrimaryKey(clonedKey.Properties);
                }

                key.GetAnnotations().ForEach(annotation => clonedKey[annotation.Name] = annotation.Value);
            }
        }
        protected IMutableKey CreateKey(IMutableEntityType entityType, int startingPropertyIndex = -1, int propertyCount = 1)
        {
            if (startingPropertyIndex == -1)
            {
                startingPropertyIndex = entityType.PropertyCount() - 1;
            }

            var keyProperties = new IMutableProperty[propertyCount];

            for (var i = 0; i < propertyCount; i++)
            {
                keyProperties[i]            = entityType.GetOrAddProperty("P" + (startingPropertyIndex + i), typeof(int?));
                keyProperties[i].IsNullable = false;
            }

            return(entityType.AddKey(keyProperties));
        }