/// <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 void ConflictingValueGenerationStrategiesWarning(
            [NotNull] this IDiagnosticsLogger <DbLoggerCategory.Model.Validation> diagnostics,
            SqlServerValueGenerationStrategy sqlServerValueGenerationStrategy,
            [NotNull] string otherValueGenerationStrategy,
            [NotNull] IProperty property)
        {
            var definition = SqlServerResources.LogConflictingValueGenerationStrategies(diagnostics);

            if (diagnostics.ShouldLog(definition))
            {
                definition.Log(diagnostics, sqlServerValueGenerationStrategy.ToString(), otherValueGenerationStrategy,
                               property.Name, property.DeclaringEntityType.DisplayName());
            }

            if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
            {
                var eventData = new ConflictingValueGenerationStrategiesEventData(
                    definition,
                    ConflictingValueGenerationStrategiesWarning,
                    sqlServerValueGenerationStrategy,
                    otherValueGenerationStrategy,
                    property);

                diagnostics.DispatchEventData(definition, eventData, diagnosticSourceEnabled, simpleLogEnabled);
            }
        }
        protected virtual bool SetValueGenerationStrategy(SqlServerValueGenerationStrategy? value)
        {
            if (value != null)
            {
                var propertyType = Property.ClrType;

                if (value == SqlServerValueGenerationStrategy.IdentityColumn
                    && (!propertyType.IsInteger()
                        || propertyType == typeof(byte)
                        || propertyType == typeof(byte?)))
                {
                    throw new ArgumentException(SqlServerStrings.IdentityBadType(
                        Property.Name, Property.DeclaringEntityType.Name, propertyType.Name));
                }

                if ((value == SqlServerValueGenerationStrategy.SequenceHiLo)
                    && !propertyType.IsInteger())
                {
                    throw new ArgumentException(SqlServerStrings.SequenceBadType(
                        Property.Name, Property.DeclaringEntityType.Name, propertyType.Name));
                }
            }

            return Annotations.SetAnnotation(SqlServerFullAnnotationNames.Instance.ValueGenerationStrategy, null, value);
        }
 /// <summary>
 ///     Constructs the event payload.
 /// </summary>
 /// <param name="eventDefinition">The event definition.</param>
 /// <param name="messageGenerator">A delegate that generates a log message for this event.</param>
 /// <param name="sqlServerValueGenerationStrategy">The SQL Server value generation strategy.</param>
 /// <param name="otherValueGenerationStrategy">The other value generation strategy.</param>
 /// <param name="property">The property.</param>
 public ConflictingValueGenerationStrategiesEventData(
     EventDefinitionBase eventDefinition,
     Func <EventDefinitionBase, EventData, string> messageGenerator,
     SqlServerValueGenerationStrategy sqlServerValueGenerationStrategy,
     string otherValueGenerationStrategy,
     IReadOnlyProperty property)
     : base(eventDefinition, messageGenerator)
 {
     SqlServerValueGenerationStrategy = sqlServerValueGenerationStrategy;
     OtherValueGenerationStrategy     = otherValueGenerationStrategy;
     Property = property;
 }
Пример #4
0
        public void SqlServerValueGenerationStrategy_warns_when_setting_conflicting_value_generation_strategies(
            SqlServerValueGenerationStrategy sqlServerValueGenerationStrategy, string conflictingValueGenerationStrategy)
        {
            var modelBuilder = CreateConventionalModelBuilder();

            var propertyBuilder = modelBuilder.Entity <Dog>().Property <int>("Id");

            propertyBuilder.Metadata.SetValueGenerationStrategy(sqlServerValueGenerationStrategy);
            ConfigureProperty(propertyBuilder.Metadata, conflictingValueGenerationStrategy, "NEXT VALUE FOR [Id]");

            VerifyWarning(
                SqlServerResources.LogConflictingValueGenerationStrategies(new TestLogger <SqlServerLoggingDefinitions>())
                .GenerateMessage(sqlServerValueGenerationStrategy.ToString(), conflictingValueGenerationStrategy, "Id", nameof(Dog)),
                modelBuilder.Model);
        }
Пример #5
0
        public void SqlServerValueGenerationStrategy_warns_when_setting_conflicting_DefaultValue(
            SqlServerValueGenerationStrategy sqlServerValueGenerationStrategy)
        {
            var modelBuilder = CreateConventionalModelBuilder();

            var propertyBuilder = modelBuilder.Entity <Dog>().Property <int>("Id");

            propertyBuilder.Metadata.SetValueGenerationStrategy(sqlServerValueGenerationStrategy);
            ConfigureProperty(propertyBuilder.Metadata, "DefaultValue", "2");

            VerifyWarnings(new[] {
                SqlServerResources.LogConflictingValueGenerationStrategies(new TestLogger <SqlServerLoggingDefinitions>())
                .GenerateMessage(sqlServerValueGenerationStrategy.ToString(), "DefaultValue", "Id", nameof(Dog)),
                RelationalResources.LogKeyHasDefaultValue(new TestLogger <SqlServerLoggingDefinitions>())
                .GenerateMessage("Id", nameof(Dog))
            },
                           modelBuilder.Model);
        }
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public new virtual bool ValueGenerationStrategy(SqlServerValueGenerationStrategy? value) => SetValueGenerationStrategy(value);
 protected virtual bool SetValueGenerationStrategy(SqlServerValueGenerationStrategy? value)
     => Annotations.SetAnnotation(SqlServerAnnotationNames.ValueGenerationStrategy, value);
        protected virtual bool SetValueGenerationStrategy(SqlServerValueGenerationStrategy? value)
        {
            if (value != null)
            {
                var propertyType = Property.ClrType;

                if (value == SqlServerValueGenerationStrategy.IdentityColumn
                    && propertyType != typeof(decimal)
                    && (!propertyType.IsInteger()
                        || propertyType == typeof(byte)
                        || propertyType == typeof(byte?)))
                {
                    throw new ArgumentException(SqlServerStrings.IdentityBadType(
                        Property.Name, Property.DeclaringEntityType.Name, propertyType.Name));
                }

                if ((value == SqlServerValueGenerationStrategy.SequenceHiLo)
                    && !propertyType.IsInteger())
                {
                    throw new ArgumentException(SqlServerStrings.SequenceBadType(
                        Property.Name, Property.DeclaringEntityType.Name, propertyType.Name));
                }
            }

            if (!CanSetValueGenerationStrategy(value))
            {
                return false;
            }

            if (!ShouldThrowOnConflict
                && ValueGenerationStrategy != value
                && value != null)
            {
                ClearAllServerGeneratedValues();
            }

            return Annotations.SetAnnotation(SqlServerFullAnnotationNames.Instance.ValueGenerationStrategy, null, value);
        }
        protected virtual bool CanSetValueGenerationStrategy(SqlServerValueGenerationStrategy? value)
        {
            if (GetSqlServerValueGenerationStrategy(fallbackToModel: false) == value)
            {
                return true;
            }

            if (!Annotations.CanSetAnnotation(SqlServerFullAnnotationNames.Instance.ValueGenerationStrategy, null, value))
            {
                return false;
            }

            if (ShouldThrowOnConflict)
            {
                if (GetDefaultValue(false) != null)
                {
                    throw new InvalidOperationException(
                        RelationalStrings.ConflictingColumnServerGeneration(nameof(ValueGenerationStrategy), Property.Name, nameof(DefaultValue)));
                }
                if (GetDefaultValueSql(false) != null)
                {
                    throw new InvalidOperationException(
                        RelationalStrings.ConflictingColumnServerGeneration(nameof(ValueGenerationStrategy), Property.Name, nameof(DefaultValueSql)));
                }
                if (GetComputedColumnSql(false) != null)
                {
                    throw new InvalidOperationException(
                        RelationalStrings.ConflictingColumnServerGeneration(nameof(ValueGenerationStrategy), Property.Name, nameof(ComputedColumnSql)));
                }
            }
            else if (value != null
                     && (!CanSetDefaultValue(null)
                         || !CanSetDefaultValueSql(null)
                         || !CanSetComputedColumnSql(null)))
            {
                return false;
            }

            return true;
        }
 protected virtual bool SetValueGenerationStrategy(SqlServerValueGenerationStrategy? value)
     => Annotations.SetAnnotation(SqlServerFullAnnotationNames.Instance.ValueGenerationStrategy,
         null,
         value);