protected IMutableForeignKey CreateForeignKey(
            IMutableEntityType dependEntityType, IReadOnlyList <IMutableProperty> dependentProperties, IMutableKey principalKey)
        {
            var foreignKey = dependEntityType.AddForeignKey(dependentProperties, principalKey, principalKey.DeclaringEntityType);

            foreignKey.IsUnique = true;

            return(foreignKey);
        }
예제 #2
0
        /// <summary>
        ///     Adds a new relationship to this entity.
        /// </summary>
        /// <param name="entityType"> The entity type to add the foreign key to. </param>
        /// <param name="property"> The property that the foreign key is defined on. </param>
        /// <param name="principalKey"> The primary or alternate key that is referenced. </param>
        /// <param name="principalEntityType">
        ///     The entity type that the relationship targets. This may be different from the type that <paramref name="principalKey" />
        ///     is defined on when the relationship targets a derived type in an inheritance hierarchy (since the key is defined on the
        ///     base type of the hierarchy).
        /// </param>
        /// <returns> The newly created foreign key. </returns>
        public static IMutableForeignKey AddForeignKey(
            [NotNull] this IMutableEntityType entityType,
            [NotNull] IMutableProperty property,
            [NotNull] IMutableKey principalKey,
            [NotNull] IMutableEntityType principalEntityType)
        {
            Check.NotNull(entityType, nameof(entityType));

            return(entityType.AddForeignKey(new[] { property }, principalKey, principalEntityType));
        }
예제 #3
0
        /// <summary>
        ///     Gets an existing relationship, or creates a new one if one is not already defined.
        /// </summary>
        /// <param name="entityType"> The entity type to get or add the foreign key to. </param>
        /// <param name="properties"> The properties that the foreign key is defined on. </param>
        /// <param name="principalKey"> The primary or alternate key that is referenced. </param>
        /// <param name="principalEntityType">
        ///     The entity type that the relationship targets. This may be different from the type that <paramref name="principalKey" />
        ///     is defined on when the relationship targets a derived type in an inheritance hierarchy (since the key is defined on the
        ///     base type of the hierarchy).
        /// </param>
        /// <returns> The existing or newly created foreign key. </returns>
        public static IMutableForeignKey GetOrAddForeignKey(
            [NotNull] this IMutableEntityType entityType,
            [NotNull] IReadOnlyList <IMutableProperty> properties,
            [NotNull] IMutableKey principalKey,
            [NotNull] IMutableEntityType principalEntityType)
        {
            Check.NotNull(entityType, nameof(entityType));

            return(entityType.FindForeignKey(properties, principalKey, principalEntityType)
                   ?? entityType.AddForeignKey(properties, principalKey, principalEntityType));
        }
예제 #4
0
 private static void CloneForeignKeys(IReadOnlyEntityType sourceEntityType, IMutableEntityType targetEntityType)
 {
     foreach (var foreignKey in sourceEntityType.GetDeclaredForeignKeys())
     {
         var targetPrincipalEntityType = targetEntityType.Model.FindEntityType(foreignKey.PrincipalEntityType.Name);
         var clonedForeignKey          = targetEntityType.AddForeignKey(
             foreignKey.Properties.Select(p => targetEntityType.FindProperty(p.Name)).ToList(),
             targetPrincipalEntityType.FindKey(
                 foreignKey.PrincipalKey.Properties.Select(p => targetPrincipalEntityType.FindProperty(p.Name)).ToList()),
             targetPrincipalEntityType);
         clonedForeignKey.IsUnique   = foreignKey.IsUnique;
         clonedForeignKey.IsRequired = foreignKey.IsRequired;
         foreignKey.GetAnnotations().ForEach(annotation => clonedForeignKey[annotation.Name] = annotation.Value);
     }
 }
예제 #5
0
        public void Marking_a_property_ValueGenerated_throws_if_part_of_a_key_and_inherited_foreign_key()
        {
            var model       = new Model();
            var baseType    = model.AddEntityType(typeof(BaseType));
            var idProperty  = baseType.GetOrAddProperty(nameof(Customer.Id), typeof(int));
            var idProperty2 = baseType.GetOrAddProperty("id2", typeof(int));
            var key         = baseType.GetOrAddKey(new[] { idProperty, idProperty2 });
            IMutableEntityType entityType = model.AddEntityType(typeof(Customer));

            entityType.BaseType = baseType;
            var fkProperty = entityType.AddProperty("fk", typeof(int));

            entityType.AddForeignKey(new[] { fkProperty, idProperty }, key, entityType);

            Assert.Equal(
                CoreStrings.ForeignKeyPropertyInKey(
                    nameof(Customer.Id),
                    typeof(Customer).Name,
                    "{'" + nameof(Customer.Id) + "'" + ", 'id2'}",
                    typeof(BaseType).Name),
                Assert.Throws <InvalidOperationException>(() => idProperty.ValueGenerated = ValueGenerated.OnAdd).Message);
        }