예제 #1
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public static bool RequiresValueGenerator(this IReadOnlyProperty property)
 => (property.ValueGenerated.ForAdd() &&
     property.IsKey() &&
     (!property.IsForeignKey() ||
      property.IsForeignKeyToSelf() ||
      (property.GetContainingForeignKeys().All(fk => fk.Properties.Any(p => p != property && p.IsNullable))))) ||
 property.GetValueGeneratorFactory() != null;
예제 #2
0
        public static bool IsForeignKeyToSelf([NotNull] this IReadOnlyProperty property)
        {
            Check.DebugAssert(property.IsKey(), "Only call this method for properties known to be part of a key.");

            foreach (var foreignKey in property.GetContainingForeignKeys())
            {
                var propertyIndex = foreignKey.Properties.IndexOf(property);
                if (propertyIndex == foreignKey.PrincipalKey.Properties.IndexOf(property))
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #3
0
        /// <summary>
        ///     <para>
        ///         Returns the <see cref="MySqlValueGenerationStrategy" /> to use for the property.
        ///     </para>
        ///     <para>
        ///         If no strategy is set for the property, then the strategy to use will be taken from the <see cref="IModel" />.
        ///     </para>
        /// </summary>
        /// <returns> The strategy, or <see cref="MySqlValueGenerationStrategy.None"/> if none was set. </returns>
        public static MySqlValueGenerationStrategy?GetValueGenerationStrategy([NotNull] this IReadOnlyProperty property, StoreObjectIdentifier storeObject = default)
        {
            var annotation = property[MySqlAnnotationNames.ValueGenerationStrategy];

            if (annotation != null)
            {
                // Allow users to use the underlying type value instead of the enum itself.
                // Workaround for: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1205
                //return ObjectToEnumConverter.GetEnumValue<MySqlValueGenerationStrategy>(annotation);
                return(ObjectToEnumConverter.GetEnumValue <MySqlValueGenerationStrategy>(annotation));
            }

            if (property.GetContainingForeignKeys().Any(fk => !fk.IsBaseLinking()) ||
                property.TryGetDefaultValue(storeObject, out _) ||
                property.GetDefaultValueSql() != null ||
                property.GetComputedColumnSql() != null)
            {
                return(null);
            }

            if (storeObject != default &&
                property.ValueGenerated == ValueGenerated.Never)
            {
                return(property.FindSharedStoreObjectRootProperty(storeObject)
                       ?.GetValueGenerationStrategy(storeObject));
            }

            if (property.ValueGenerated == ValueGenerated.OnAdd &&
                IsCompatibleIdentityColumn(property))
            {
                return(MySqlValueGenerationStrategy.IdentityColumn);
            }

            if (property.ValueGenerated == ValueGenerated.OnAddOrUpdate &&
                IsCompatibleComputedColumn(property))
            {
                return(MySqlValueGenerationStrategy.ComputedColumn);
            }

            return(null);
        }
예제 #4
0
 /// <summary>
 ///     Returns the store value generation strategy to set for the given property.
 /// </summary>
 /// <param name="property"> The property. </param>
 /// <returns> The store value generation strategy to set for the given property. </returns>
 public static ValueGenerated?GetValueGenerated(IReadOnlyProperty property)
 => !property.GetContainingForeignKeys().Any(fk => !fk.IsBaseLinking()) &&
 ShouldHaveGeneratedProperty(property.FindContainingPrimaryKey()) &&
 CanBeGenerated(property)
             ? ValueGenerated.OnAdd
             : (ValueGenerated?)null;