Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
Expand All @@ -14,6 +15,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.Logging;

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
Expand Down Expand Up @@ -636,10 +638,15 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations)
$"({_code.Literal(property.Relational().ComputedColumnSql)})");
}

var dummyLogger = new DiagnosticsLogger<DbLoggerCategory.Model>(
new LoggerFactory(),
new LoggingOptions(),
new DiagnosticListener(""));

var valueGenerated = property.ValueGenerated;
var isRowVersion = false;
if (((Property)property).GetValueGeneratedConfigurationSource().HasValue
&& new RelationalValueGeneratorConvention().GetValueGenerated((Property)property) != valueGenerated)
&& new RelationalValueGeneratorConvention(dummyLogger).GetValueGenerated((Property)property) != valueGenerated)
{
string methodName;
switch (valueGenerated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using JetBrains.Annotations;
Expand All @@ -18,6 +19,7 @@
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.Logging;
using ScaffoldingAnnotationNames = Microsoft.EntityFrameworkCore.Scaffolding.Metadata.Internal.ScaffoldingAnnotationNames;

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
Expand Down Expand Up @@ -476,14 +478,20 @@ protected virtual KeyBuilder VisitPrimaryKey([NotNull] EntityTypeBuilder builder

var keyBuilder = builder.HasKey(primaryKey.Columns.Select(GetPropertyName).ToArray());


if (primaryKey.Columns.Count == 1
&& primaryKey.Columns[0].ValueGenerated == null
&& primaryKey.Columns[0].DefaultValueSql == null)
{
var property = builder.Metadata.FindProperty(GetPropertyName(primaryKey.Columns[0]))?.AsProperty();
if (property != null)
{
var conventionalValueGenerated = new RelationalValueGeneratorConvention().GetValueGenerated(property);
var dummyLogger = new DiagnosticsLogger<DbLoggerCategory.Model>(
new LoggerFactory(),
new LoggingOptions(),
new DiagnosticListener(""));

var conventionalValueGenerated = new RelationalValueGeneratorConvention(dummyLogger).GetValueGenerated(property);
if (conventionalValueGenerated == ValueGenerated.OnAdd)
{
property.ValueGenerated = ValueGenerated.Never;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -23,6 +25,21 @@ namespace Microsoft.EntityFrameworkCore.InMemory.Metadata.Conventions.Internal
/// </summary>
public class InMemoryConventionSetBuilder : IConventionSetBuilder
{
/// <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 InMemoryConventionSetBuilder([NotNull] IDiagnosticsLogger<DbLoggerCategory.Model> logger)
{
Logger = logger;
}

/// <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>
protected virtual IDiagnosticsLogger<DbLoggerCategory.Model> Logger { get; }

/// <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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
Expand All @@ -28,6 +29,7 @@ public class ProxiesConventionSetBuilder : IConventionSetBuilder
private readonly IDbContextOptions _options;
private readonly IConstructorBindingFactory _constructorBindingFactory;
private readonly IProxyFactory _proxyFactory;
private readonly IDiagnosticsLogger<DbLoggerCategory.Model> _logger;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
Expand All @@ -36,11 +38,13 @@ public class ProxiesConventionSetBuilder : IConventionSetBuilder
public ProxiesConventionSetBuilder(
[NotNull] IDbContextOptions options,
[NotNull] IConstructorBindingFactory constructorBindingFactory,
[NotNull] IProxyFactory proxyFactory)
[NotNull] IProxyFactory proxyFactory,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Model> logger)
{
_options = options;
_constructorBindingFactory = constructorBindingFactory;
_proxyFactory = proxyFactory;
_logger = logger;
}

/// <summary>
Expand All @@ -53,6 +57,7 @@ public virtual ConventionSet AddConventions(ConventionSet conventionSet)
new ProxyBindingRewriter(
_proxyFactory,
_constructorBindingFactory,
_logger,
_options.FindExtension<ProxiesOptionsExtension>()));

return conventionSet;
Expand Down
4 changes: 3 additions & 1 deletion src/EFCore.Proxies/Proxies/Internal/ProxyBindingRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
Expand Down Expand Up @@ -36,9 +37,10 @@ private static readonly PropertyInfo _lazyLoaderProperty
public ProxyBindingRewriter(
[NotNull] IProxyFactory proxyFactory,
[NotNull] IConstructorBindingFactory bindingFactory,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Model> logger,
[CanBeNull] ProxiesOptionsExtension options)
{
_directBindingConvention = new ConstructorBindingConvention(bindingFactory);
_directBindingConvention = new ConstructorBindingConvention(bindingFactory, logger);
_proxyFactory = proxyFactory;
_options = options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public static readonly IDictionary<Type, ServiceCharacteristics> RelationalServi
{ typeof(IMigrationsAnnotationProvider), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMigrationCommandExecutor), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalCommandBuilderFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRawSqlCommandBuilder), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMigrationsSqlGenerator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalTypeMappingSource), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalValueBufferFactoryFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMaterializerFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
Expand All @@ -81,6 +79,8 @@ public static readonly IDictionary<Type, ServiceCharacteristics> RelationalServi
{ typeof(ICommandBatchPreparer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IModificationCommandBatchFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsModelDiffer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsSqlGenerator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRawSqlCommandBuilder), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsAssembly), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IBatchExecutor), new ServiceCharacteristics(ServiceLifetime.Scoped) },
Expand Down Expand Up @@ -177,15 +177,16 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
.AddDependencySingleton<UpdateSqlGeneratorDependencies>()
.AddDependencySingleton<QuerySqlGeneratorDependencies>()
.AddDependencySingleton<RelationalCompositeMethodCallTranslatorDependencies>()
.AddDependencySingleton<MigrationsSqlGeneratorDependencies>()
.AddDependencySingleton<MigrationsAnnotationProviderDependencies>()
.AddDependencySingleton<SqlTranslatingExpressionVisitorDependencies>()
.AddDependencySingleton<ParameterNameGeneratorDependencies>()
.AddDependencySingleton<SelectExpressionDependencies>()
.AddDependencySingleton<RelationalValueBufferFactoryDependencies>()
.AddDependencySingleton<RelationalProjectionExpressionVisitorDependencies>()
.AddDependencySingleton<RelationalTransactionFactoryDependencies>()
.AddDependencyScoped<MigrationsSqlGeneratorDependencies>()
.AddDependencyScoped<RelationalConventionSetBuilderDependencies>()
.AddDependencyScoped<ModificationCommandBatchFactoryDependencies>()
.AddDependencyScoped<CommandBatchPreparerDependencies>()
.AddDependencyScoped<RelationalDatabaseCreatorDependencies>()
.AddDependencyScoped<HistoryRepositoryDependencies>()
Expand Down
54 changes: 30 additions & 24 deletions src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand Down Expand Up @@ -49,22 +50,23 @@ public RelationalModelValidator(
/// Validates a model, throwing an exception if any errors are found.
/// </summary>
/// <param name="model"> The model to validate. </param>
public override void Validate(IModel model)
/// <param name="loggers"> Loggers to use. </param>
public override void Validate(IModel model, DiagnosticsLoggers loggers)
{
base.Validate(model);
base.Validate(model, loggers);

ValidateSharedTableCompatibility(model);
ValidateInheritanceMapping(model);
ValidateDefaultValuesOnKeys(model);
ValidateBoolsWithDefaults(model);
ValidateDbFunctions(model);
ValidateSharedTableCompatibility(model, loggers);
ValidateInheritanceMapping(model, loggers);
ValidateDefaultValuesOnKeys(model, loggers);
ValidateBoolsWithDefaults(model, loggers);
ValidateDbFunctions(model, loggers);
}

/// <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>
protected virtual void ValidateDbFunctions([NotNull] IModel model)
protected virtual void ValidateDbFunctions([NotNull] IModel model, DiagnosticsLoggers loggers)
{
foreach (var dbFunction in model.Relational().DbFunctions)
{
Expand Down Expand Up @@ -105,18 +107,20 @@ protected virtual void ValidateDbFunctions([NotNull] IModel model)
/// 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>
protected virtual void ValidateBoolsWithDefaults([NotNull] IModel model)
protected virtual void ValidateBoolsWithDefaults([NotNull] IModel model, DiagnosticsLoggers loggers)
{
Check.NotNull(model, nameof(model));

var logger = loggers.GetLogger<DbLoggerCategory.Model.Validation>();

foreach (var property in model.GetEntityTypes().SelectMany(e => e.GetDeclaredProperties()))
{
if (property.ClrType == typeof(bool)
&& property.ValueGenerated != ValueGenerated.Never
&& (IsNotNullAndFalse(property.Relational().DefaultValue)
|| property.Relational().DefaultValueSql != null))
{
Dependencies.Logger.BoolWithDefaultWarning(property);
logger.BoolWithDefaultWarning(property);
}
}
}
Expand All @@ -129,21 +133,23 @@ private static bool IsNotNullAndFalse(object value)
/// 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>
protected virtual void ValidateDefaultValuesOnKeys([NotNull] IModel model)
protected virtual void ValidateDefaultValuesOnKeys([NotNull] IModel model, DiagnosticsLoggers loggers)
{
var logger = loggers.GetLogger<DbLoggerCategory.Model.Validation>();

foreach (var property in model.GetEntityTypes().SelectMany(
t => t.GetDeclaredKeys().SelectMany(k => k.Properties))
.Where(p => p.Relational().DefaultValue != null))
{
Dependencies.Logger.ModelValidationKeyDefaultValueWarning(property);
logger.ModelValidationKeyDefaultValueWarning(property);
}
}

/// <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>
protected virtual void ValidateSharedTableCompatibility([NotNull] IModel model)
protected virtual void ValidateSharedTableCompatibility([NotNull] IModel model, DiagnosticsLoggers loggers)
{
var tables = new Dictionary<string, List<IEntityType>>();
foreach (var entityType in model.GetEntityTypes().Where(et => !et.IsQueryType))
Expand All @@ -164,11 +170,11 @@ protected virtual void ValidateSharedTableCompatibility([NotNull] IModel model)
{
var mappedTypes = tableMapping.Value;
var tableName = tableMapping.Key;
ValidateSharedTableCompatibility(mappedTypes, tableName);
ValidateSharedColumnsCompatibility(mappedTypes, tableName);
ValidateSharedKeysCompatibility(mappedTypes, tableName);
ValidateSharedForeignKeysCompatibility(mappedTypes, tableName);
ValidateSharedIndexesCompatibility(mappedTypes, tableName);
ValidateSharedTableCompatibility(mappedTypes, tableName, loggers);
ValidateSharedColumnsCompatibility(mappedTypes, tableName, loggers);
ValidateSharedKeysCompatibility(mappedTypes, tableName, loggers);
ValidateSharedForeignKeysCompatibility(mappedTypes, tableName, loggers);
ValidateSharedIndexesCompatibility(mappedTypes, tableName, loggers);
}
}

Expand All @@ -177,7 +183,7 @@ protected virtual void ValidateSharedTableCompatibility([NotNull] IModel model)
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
protected virtual void ValidateSharedTableCompatibility(
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName)
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName, DiagnosticsLoggers loggers)
{
if (mappedTypes.Count == 1)
{
Expand Down Expand Up @@ -276,7 +282,7 @@ private static bool IsIdentifyingPrincipal(IEntityType dependentEntityType, IEnt
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
protected virtual void ValidateSharedColumnsCompatibility(
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName)
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName, DiagnosticsLoggers loggers)
{
var propertyMappings = new Dictionary<string, IProperty>();

Expand Down Expand Up @@ -377,7 +383,7 @@ protected virtual void ValidateSharedColumnsCompatibility(
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
protected virtual void ValidateSharedForeignKeysCompatibility(
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName)
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName, DiagnosticsLoggers loggers)
{
var foreignKeyMappings = new Dictionary<string, IForeignKey>();

Expand All @@ -399,7 +405,7 @@ protected virtual void ValidateSharedForeignKeysCompatibility(
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
protected virtual void ValidateSharedIndexesCompatibility(
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName)
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName, DiagnosticsLoggers loggers)
{
var indexMappings = new Dictionary<string, IIndex>();

Expand All @@ -421,7 +427,7 @@ protected virtual void ValidateSharedIndexesCompatibility(
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
protected virtual void ValidateSharedKeysCompatibility(
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName)
[NotNull] IReadOnlyList<IEntityType> mappedTypes, [NotNull] string tableName, DiagnosticsLoggers loggers)
{
var keyMappings = new Dictionary<string, IKey>();

Expand Down Expand Up @@ -456,7 +462,7 @@ protected virtual void ValidateSharedKeysCompatibility(
/// 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>
protected virtual void ValidateInheritanceMapping([NotNull] IModel model)
protected virtual void ValidateInheritanceMapping([NotNull] IModel model, DiagnosticsLoggers loggers)
{
foreach (var rootEntityType in model.GetRootEntityTypes())
{
Expand Down
Loading