/// <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) { return((MySqlValueGenerationStrategy?)annotation); } if (property.GetDefaultValue() != null || property.GetDefaultValueSql() != null || property.GetComputedColumnSql() != null) { return(null); } if (storeObject != default && property.ValueGenerated == ValueGenerated.Never) { return(property.FindSharedStoreObjectRootProperty(storeObject) ?.GetValueGenerationStrategy(storeObject)); } if (IsCompatibleIdentityColumn(property) && property.ValueGenerated == ValueGenerated.OnAdd) { return(MySqlValueGenerationStrategy.IdentityColumn); } if (IsCompatibleComputedColumn(property) && property.ValueGenerated == ValueGenerated.OnAddOrUpdate) { return(MySqlValueGenerationStrategy.ComputedColumn); } return(null); }
static bool IsStrategyNoneNeeded(IReadOnlyProperty property, StoreObjectIdentifier storeObject) { if (property.ValueGenerated == ValueGenerated.OnAdd && property.GetDefaultValue(storeObject) == null && property.GetDefaultValueSql(storeObject) == null && property.GetComputedColumnSql(storeObject) == null && property.DeclaringEntityType.Model.GetValueGenerationStrategy() != NpgsqlValueGenerationStrategy.None) { var providerClrType = (property.GetValueConverter() ?? property.FindRelationalTypeMapping(storeObject)?.Converter) ?.ProviderClrType.UnwrapNullableType(); return(providerClrType != null && (providerClrType.IsInteger())); } return(false); }
/// <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); }