Exemple #1
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);
        }
Exemple #2
0
        public static MySqlValueGenerationStrategy?GetValueGenerationStrategy(IAnnotation[] annotations)
        {
            var valueGenerationStrategy = ObjectToEnumConverter.GetEnumValue <MySqlValueGenerationStrategy>(
                annotations.FirstOrDefault(a => a.Name == MySqlAnnotationNames.ValueGenerationStrategy)?.Value);

            if (!valueGenerationStrategy.HasValue ||
                valueGenerationStrategy == MySqlValueGenerationStrategy.None)
            {
                var generatedOnAddAnnotation = annotations.FirstOrDefault(a => a.Name == MySqlAnnotationNames.LegacyValueGeneratedOnAdd)?.Value;
                if (generatedOnAddAnnotation != null && (bool)generatedOnAddAnnotation)
                {
                    valueGenerationStrategy = MySqlValueGenerationStrategy.IdentityColumn;
                }

                var generatedOnAddOrUpdateAnnotation = annotations.FirstOrDefault(a => a.Name == MySqlAnnotationNames.LegacyValueGeneratedOnAddOrUpdate)?.Value;
                if (generatedOnAddOrUpdateAnnotation != null && (bool)generatedOnAddOrUpdateAnnotation)
                {
                    valueGenerationStrategy = MySqlValueGenerationStrategy.ComputedColumn;
                }
            }

            return(valueGenerationStrategy);
        }
 /// <summary>
 ///     Returns the character set delegation modes for the model/database.
 /// </summary>
 /// <param name="model"> The model. </param>
 /// <returns> The character set delegation modes. </returns>
 public static DelegationModes?GetCharSetDelegation([NotNull] this IModel model)
 => ObjectToEnumConverter.GetEnumValue <DelegationModes>(model[MySqlAnnotationNames.CharSetDelegation]) ??
 (model[MySqlAnnotationNames.CharSetDelegation] is bool explicitlyDelegateToChildren
 /// <summary>
 ///     Returns the character set delegation modes for the entity/table.
 /// </summary>
 /// <param name="entityType"> The entity type. </param>
 /// <returns> The character set delegation modes. </returns>
 public static DelegationModes?GetCharSetDelegation([NotNull] this IReadOnlyEntityType entityType)
 => ObjectToEnumConverter.GetEnumValue <DelegationModes>(entityType[MySqlAnnotationNames.CharSetDelegation]) ??
 (entityType[MySqlAnnotationNames.CharSetDelegation] is bool explicitlyDelegateToChildren