Esempio n. 1
0
        /// <summary>
        ///     Sets the primary key for this entity.
        /// </summary>
        /// <param name="properties"> The properties that make up the primary key. </param>
        /// <returns> The newly created key. </returns>
        internal Key SetPrimaryKey(IReadOnlyList <Property> properties)
        {
            Check.NotEmpty(properties, nameof(properties));
            Check.HasNoNulls(properties, nameof(properties));

            if (_baseType != null)
            {
                throw new InvalidOperationException(ResX.DerivedEntityKey(Name, _baseType.Name));
            }

            if (_primaryKey != null)
            {
                throw new InvalidOperationException(ResX.PrimaryKeyAlreadyExists);
            }

            Key key = GetOrAddKey(properties);

            foreach (var property in key.Properties)
            {
                _properties.Remove(property.Name);
                property.PrimaryKey = key;
            }

            _primaryKey = key;

            foreach (var property in key.Properties)
            {
                _properties.Add(property.Name, property);
            }

            return(_primaryKey);
        }
Esempio n. 2
0
        /// <summary>
        ///     Adds a new alternate key to this entity type.
        /// </summary>
        /// <param name="properties"> The properties that make up the alternate key. </param>
        /// <returns> The newly created key. </returns>
        internal Key AddKey(IReadOnlyList <Property> properties)
        {
            Check.NotEmpty(properties, nameof(properties));
            Check.HasNoNulls(properties, nameof(properties));

            if (_baseType != null)
            {
                throw new InvalidOperationException(ResX.DerivedEntityKey(Name, _baseType.Name));
            }

            foreach (var property in properties)
            {
                if (FindProperty(property.Name) != property)
                {
                    throw new InvalidOperationException(ResX.KeyPropertiesWrongEntity(Property.Format(properties), Name));
                }

                if (property.ForeignKeys != null && property.ForeignKeys.Any(k => k.DeclaringEntity != this))
                {
                    throw new InvalidOperationException(ResX.KeyPropertyInForeignKey(property.Name, Name));
                }

                if (property.IsNullable)
                {
                    throw new InvalidOperationException(ResX.NullableKey(Name, property.Name));
                }
            }

            var key = FindKey(properties);

            if (key != null)
            {
                throw new InvalidOperationException(ResX.DuplicateKey(Property.Format(properties), Name, key.DeclaringEntity.Name));
            }

            key = new Key(properties);
            _keys.Add(properties, key);

            foreach (var property in properties)
            {
                var currentKeys = property.Keys;
                if (currentKeys == null)
                {
                    property.Keys = new Key[] { key };
                }
                else
                {
                    var newKeys = currentKeys.ToList();
                    newKeys.Add(key);
                    property.Keys = newKeys;
                }
            }

            return(Model.ConventionDispatcher.OnKeyAdded(key));
        }