Exemple #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 void ConflictingValueGenerationStrategiesWarning(
            [NotNull] this IDiagnosticsLogger <DbLoggerCategory.Model.Validation> diagnostics,
            SqlServerValueGenerationStrategy sqlServerValueGenerationStrategy,
            [NotNull] string otherValueGenerationStrategy,
            [NotNull] IReadOnlyProperty 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);
            }
        }
Exemple #2
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);
        }
Exemple #3
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);
        }
        public virtual void SqlServerValueGeneration_conflicting_with_existing_ValueGeneration_strategy_throws()
        {
            var modelBuilder = CreateModelBuilder();

            modelBuilder.Entity <Fred>()
            .Property(e => e.Id)
            .HasDefaultValueSql("2")
            .UseHiLo();

            Assert.Equal(
                CoreStrings.WarningAsErrorTemplate(
                    SqlServerEventId.ConflictingValueGenerationStrategiesWarning,
                    SqlServerResources.LogConflictingValueGenerationStrategies(
                        new TestLogger <SqlServerLoggingDefinitions>())
                    .GenerateMessage(SqlServerValueGenerationStrategy.SequenceHiLo.ToString(), "DefaultValueSql", "Id", nameof(Fred)),
                    "SqlServerEventId.ConflictingValueGenerationStrategiesWarning"),
                Assert.Throws <InvalidOperationException>(() => Validate(modelBuilder)).Message);
        }
        public virtual void SqlServerValueGeneration_conflicting_with_existing_ValueGeneration_strategy_warns()
        {
            var modelBuilder = CreateModelBuilder();

            modelBuilder.Entity <Fred>()
            .Property(e => e.Id)
            .HasDefaultValueSql("2")
            .UseHiLo();

            // Assert - this does not throw
            Validate(modelBuilder);

            var logEntry = Fixture.ListLoggerFactory.Log.Single(
                l => l.Level == LogLevel.Warning && l.Id == SqlServerEventId.ConflictingValueGenerationStrategiesWarning);

            Assert.Equal(SqlServerResources.LogConflictingValueGenerationStrategies(
                             new TestLogger <SqlServerLoggingDefinitions>())
                         .GenerateMessage(SqlServerValueGenerationStrategy.SequenceHiLo.ToString(), "DefaultValueSql", "Id", nameof(Fred)),
                         logEntry.Message);
        }