diff --git a/src/EFCore.Design/Design/Internal/DbContextOperations.cs b/src/EFCore.Design/Design/Internal/DbContextOperations.cs index a574248918d..78a3bcb808d 100644 --- a/src/EFCore.Design/Design/Internal/DbContextOperations.cs +++ b/src/EFCore.Design/Design/Internal/DbContextOperations.cs @@ -259,6 +259,7 @@ private IReadOnlyList ScaffoldCompiledModel( outputDir = Path.GetFullPath(Path.Combine(_projectDir, outputDir)); var scaffolder = services.GetRequiredService(); + var databaseProvider = context.GetService(); var finalModelNamespace = modelNamespace ?? GetNamespaceFromOutputPath(outputDir) ?? ""; @@ -273,6 +274,7 @@ private IReadOnlyList ScaffoldCompiledModel( UseNullableReferenceTypes = _nullable, Suffix = suffix, ForNativeAot = nativeAot, + ProviderName = databaseProvider.Name, GeneratedFileNames = generatedFileNames }); diff --git a/src/EFCore.Design/Scaffolding/CompiledModelCodeGenerationOptions.cs b/src/EFCore.Design/Scaffolding/CompiledModelCodeGenerationOptions.cs index 74774ea74dc..8b666b3a9a5 100644 --- a/src/EFCore.Design/Scaffolding/CompiledModelCodeGenerationOptions.cs +++ b/src/EFCore.Design/Scaffolding/CompiledModelCodeGenerationOptions.cs @@ -44,6 +44,12 @@ public class CompiledModelCodeGenerationOptions /// A value indicating whether the generated code should be compatible with NativeAOT. public virtual bool ForNativeAot { get; set; } + /// + /// Gets or sets the database provider name to embed in the compiled model. + /// + /// The database provider name. + public virtual string? ProviderName { get; set; } + /// /// Gets or sets the set of file names generated so far. /// diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs index b02f95d8df4..70e7775f09e 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs @@ -66,7 +66,7 @@ public virtual IReadOnlyCollection GenerateModel( var nullable = false; var scaffoldedFiles = new List(); - var assemblyAttributesCode = CreateAssemblyAttributes(options.ModelNamespace, options.ContextType, nullable); + var assemblyAttributesCode = CreateAssemblyAttributes(options.ModelNamespace, options.ContextType, options.ProviderName, nullable); var assemblyInfoFileName = UniquifyFileName(options.ContextType.ShortDisplayName() + AssemblyAttributesSuffix, options); scaffoldedFiles.Add(new ScaffoldedFile(assemblyInfoFileName, assemblyAttributesCode)); @@ -174,6 +174,7 @@ private static string GenerateHeader(SortedSet namespaces, string curren private string CreateAssemblyAttributes( string @namespace, Type contextType, + string? providerName, bool nullable) { var mainBuilder = new IndentedStringBuilder(); @@ -183,7 +184,15 @@ private string CreateAssemblyAttributes( mainBuilder .Append("[assembly: DbContextModel(typeof(").Append(_code.Reference(contextType)) - .Append("), typeof(").Append(GetModelClassName(contextType)).AppendLine("))]"); + .Append("), typeof(").Append(GetModelClassName(contextType)).Append(")"); + + if (providerName != null) + { + mainBuilder + .Append(", ProviderName = ").Append(_code.Literal(providerName)); + } + + mainBuilder.AppendLine(")]"); return GenerateHeader(namespaces, currentNamespace: "", nullable) + mainBuilder; } diff --git a/src/EFCore/Diagnostics/CoreEventId.cs b/src/EFCore/Diagnostics/CoreEventId.cs index 3ccfac94727..c7f4b9a901c 100644 --- a/src/EFCore/Diagnostics/CoreEventId.cs +++ b/src/EFCore/Diagnostics/CoreEventId.cs @@ -90,6 +90,7 @@ private enum Id ServiceProviderDebugInfo, RedundantAddServicesCallWarning, OldModelVersionWarning, + CompiledModelProviderMismatchWarning, // Model and ModelValidation events ShadowPropertyCreated = CoreBaseId + 600, @@ -491,6 +492,19 @@ private static EventId MakeInfraId(Id id) /// public static readonly EventId OldModelVersionWarning = MakeInfraId(Id.OldModelVersionWarning); + /// + /// A compiled model was found but was built for a different database provider. + /// + /// + /// + /// This event is in the category. + /// + /// + /// This event uses the payload when used with a . + /// + /// + public static readonly EventId CompiledModelProviderMismatchWarning = MakeInfraId(Id.CompiledModelProviderMismatchWarning); + private static readonly string _modelPrefix = DbLoggerCategory.Model.Name + "."; private static EventId MakeModelId(Id id) diff --git a/src/EFCore/Diagnostics/CoreLoggerExtensions.cs b/src/EFCore/Diagnostics/CoreLoggerExtensions.cs index 5054434a5ba..b11f4d65297 100644 --- a/src/EFCore/Diagnostics/CoreLoggerExtensions.cs +++ b/src/EFCore/Diagnostics/CoreLoggerExtensions.cs @@ -230,6 +230,46 @@ private static string OldModelVersion(EventDefinitionBase definition, EventData ProductInfo.GetVersion()); } + /// + /// Logs for the event. + /// + /// The diagnostics logger to use. + /// The provider name stored in the compiled model. + /// The provider name currently configured. + public static void CompiledModelProviderMismatchWarning( + this IDiagnosticsLogger diagnostics, + string compiledProviderName, + string currentProviderName) + { + var definition = CoreResources.LogCompiledModelProviderMismatch(diagnostics); + + if (diagnostics.ShouldLog(definition)) + { + definition.Log( + diagnostics, + compiledProviderName, + currentProviderName); + } + + if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled)) + { + var eventData = new ProviderMismatchEventData( + definition, + CompiledModelProviderMismatch, + compiledProviderName, + currentProviderName); + + diagnostics.DispatchEventData(definition, eventData, diagnosticSourceEnabled, simpleLogEnabled); + } + } + + private static string CompiledModelProviderMismatch(EventDefinitionBase definition, EventData payload) + { + var d = (EventDefinition)definition; + var p = (ProviderMismatchEventData)payload; + return d.GenerateMessage(p.CompiledProviderName, p.CurrentProviderName); + } + /// /// Logs for the event. /// diff --git a/src/EFCore/Diagnostics/LoggingDefinitions.cs b/src/EFCore/Diagnostics/LoggingDefinitions.cs index 04145a1fed6..30b462e0aae 100644 --- a/src/EFCore/Diagnostics/LoggingDefinitions.cs +++ b/src/EFCore/Diagnostics/LoggingDefinitions.cs @@ -106,6 +106,15 @@ public abstract class LoggingDefinitions [EntityFrameworkInternal] public EventDefinitionBase? LogOldModelVersion; + /// + /// 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. + /// + [EntityFrameworkInternal] + public EventDefinitionBase? LogCompiledModelProviderMismatch; + /// /// 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 diff --git a/src/EFCore/Diagnostics/ProviderMismatchEventData.cs b/src/EFCore/Diagnostics/ProviderMismatchEventData.cs new file mode 100644 index 00000000000..4501cbc536d --- /dev/null +++ b/src/EFCore/Diagnostics/ProviderMismatchEventData.cs @@ -0,0 +1,41 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace Microsoft.EntityFrameworkCore.Diagnostics; + + /// + /// A event payload class for provider mismatch warnings. + /// + /// + /// See Logging, events, and diagnostics for more information and examples. + /// + public class ProviderMismatchEventData : EventData + { + /// + /// Constructs the event payload. + /// + /// The event definition. + /// A delegate that generates a log message for this event. + /// The provider name stored with the model. + /// The provider name currently configured. + public ProviderMismatchEventData( + EventDefinitionBase eventDefinition, + Func messageGenerator, + string compiledProviderName, + string currentProviderName) + : base(eventDefinition, messageGenerator) + { + CompiledProviderName = compiledProviderName; + CurrentProviderName = currentProviderName; + } + + /// + /// The provider name stored with the model. + /// + public virtual string CompiledProviderName { get; } + + /// + /// The provider name currently configured. + /// + public virtual string CurrentProviderName { get; } +} diff --git a/src/EFCore/Infrastructure/DbContextModelAttribute.cs b/src/EFCore/Infrastructure/DbContextModelAttribute.cs index bebeb262a0c..553c83f0034 100644 --- a/src/EFCore/Infrastructure/DbContextModelAttribute.cs +++ b/src/EFCore/Infrastructure/DbContextModelAttribute.cs @@ -41,4 +41,9 @@ public DbContextModelAttribute( /// Gets the compiled model. /// public Type ModelType { get; } + + /// + /// Gets the database provider name used to build the compiled model. + /// + public string? ProviderName { get; set; } } diff --git a/src/EFCore/Internal/DbContextServices.cs b/src/EFCore/Internal/DbContextServices.cs index cd9b61b29d6..dfd67f8da3c 100644 --- a/src/EFCore/Internal/DbContextServices.cs +++ b/src/EFCore/Internal/DbContextServices.cs @@ -67,7 +67,22 @@ private IModel CreateModel(bool designTime) _inOnModelCreating = true; var dependencies = _scopedProvider!.GetRequiredService(); - var modelFromOptions = CoreOptions?.Model ?? FindCompiledModel(_currentContext!.Context.GetType()); + + string? mismatchedProviderName = null; + var modelFromOptions = CoreOptions?.Model; + if (modelFromOptions == null) + { + var providers = _scopedProvider!.GetService>()?.ToList(); + var providerName = providers is [var provider] ? provider.Name : null; + + modelFromOptions = FindCompiledModel(_currentContext!.Context.GetType(), providerName, out mismatchedProviderName); + + if (mismatchedProviderName != null) + { + var logger = _scopedProvider!.GetRequiredService>(); + logger.CompiledModelProviderMismatchWarning(mismatchedProviderName, providerName!); + } + } var modelVersion = modelFromOptions?.GetProductVersion(); if (modelVersion != null) @@ -105,10 +120,12 @@ private IModel CreateModel(bool designTime) _inOnModelCreating = false; } - static IModel? FindCompiledModel(Type contextType) + static IModel? FindCompiledModel(Type contextType, string? providerName, out string? mismatchedProviderName) { + mismatchedProviderName = null; var contextAssembly = contextType.Assembly; IModel? model = null; + string? firstMismatchedProvider = null; foreach (var modelAttribute in contextAssembly.GetCustomAttributes()) { if (modelAttribute.ContextType != contextType) @@ -116,6 +133,14 @@ private IModel CreateModel(bool designTime) continue; } + if (modelAttribute.ProviderName != null + && providerName != null + && modelAttribute.ProviderName != providerName) + { + firstMismatchedProvider ??= modelAttribute.ProviderName; + continue; + } + var modelType = modelAttribute.ModelType; var instanceProperty = modelType.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static); @@ -135,6 +160,11 @@ private IModel CreateModel(bool designTime) model = (IModel)instanceProperty.GetValue(null)!; } + if (model == null) + { + mismatchedProviderName = firstMismatchedProvider; + } + return model; } } diff --git a/src/EFCore/Properties/CoreStrings.Designer.cs b/src/EFCore/Properties/CoreStrings.Designer.cs index 491e64911ba..60ba4290e46 100644 --- a/src/EFCore/Properties/CoreStrings.Designer.cs +++ b/src/EFCore/Properties/CoreStrings.Designer.cs @@ -4722,6 +4722,31 @@ public static EventDefinition LogOldModelVersion(IDiagnosticsLog return (EventDefinition)definition; } + /// + /// A compiled model was found but was built for the database provider '{compiledProviderName}'. The current context is using the database provider '{currentProviderName}'. The compiled model was ignored. Regenerate the compiled model to use the correct provider. + /// + public static EventDefinition LogCompiledModelProviderMismatch(IDiagnosticsLogger logger) + { + var definition = ((LoggingDefinitions)logger.Definitions).LogCompiledModelProviderMismatch; + if (definition == null) + { + definition = NonCapturingLazyInitializer.EnsureInitialized( + ref ((LoggingDefinitions)logger.Definitions).LogCompiledModelProviderMismatch, + logger, + static logger => new EventDefinition( + logger.Options, + CoreEventId.CompiledModelProviderMismatchWarning, + LogLevel.Warning, + "CoreEventId.CompiledModelProviderMismatchWarning", + level => LoggerMessage.Define( + level, + CoreEventId.CompiledModelProviderMismatchWarning, + _resourceManager.GetString("LogCompiledModelProviderMismatch")!))); + } + + return (EventDefinition)definition; + } + /// /// {error} /// diff --git a/src/EFCore/Properties/CoreStrings.resx b/src/EFCore/Properties/CoreStrings.resx index 7c363a7b47f..edd710d181f 100644 --- a/src/EFCore/Properties/CoreStrings.resx +++ b/src/EFCore/Properties/CoreStrings.resx @@ -1068,6 +1068,10 @@ The model supplied in the context options was created with EF Core version '{oldVersion}', but the context is from version '{newVersion}'. Update the externally built model. Warning CoreEventId.OldModelVersionWarning string string + + A compiled model was found but it was built for the database provider '{compiledProviderName}'. The current context is using the database provider '{currentProviderName}'. The compiled model was ignored. Regenerate the compiled model with the correct provider. + Warning CoreEventId.CompiledModelProviderMismatchWarning string string + {error} Debug CoreEventId.OptimisticConcurrencyException Exception diff --git a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/Basic_cosmos_model/DbContextAssemblyAttributes.cs b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/Basic_cosmos_model/DbContextAssemblyAttributes.cs index c224873f6fa..b71e4f5f111 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/Basic_cosmos_model/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/Basic_cosmos_model/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Cosmos")] diff --git a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs index c224873f6fa..b71e4f5f111 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Cosmos")] diff --git a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs index c224873f6fa..b71e4f5f111 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Cosmos")] diff --git a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs index c224873f6fa..b71e4f5f111 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Cosmos")] diff --git a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs index c224873f6fa..b71e4f5f111 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Cosmos")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_provider_value_comparer/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_provider_value_comparer/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_provider_value_comparer/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_provider_value_comparer/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_type_mapping/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_type_mapping/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_type_mapping/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_type_mapping/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_value_comparer/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_value_comparer/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_value_comparer/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_value_comparer/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_value_converter/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_value_converter/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_value_converter/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Custom_value_converter/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Empty_model/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Empty_model/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Empty_model/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Empty_model/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Fully_qualified_model/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Fully_qualified_model/DbContextAssemblyAttributes.cs index 3879ce8f4f5..9793f9522ba 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Fully_qualified_model/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Fully_qualified_model/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Global_namespace/GlobalNamespaceContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Global_namespace/GlobalNamespaceContextAssemblyAttributes.cs index 5a2d96f9853..cfd694ba828 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Global_namespace/GlobalNamespaceContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Global_namespace/GlobalNamespaceContextAssemblyAttributes.cs @@ -4,4 +4,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(GlobalNamespaceContext), typeof(GlobalNamespaceContextModel))] +[assembly: DbContextModel(typeof(GlobalNamespaceContext), typeof(GlobalNamespaceContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Lazy_loading_manual/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Lazy_loading_manual/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Lazy_loading_manual/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Lazy_loading_manual/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Lazy_loading_proxies/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Lazy_loading_proxies/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Lazy_loading_proxies/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Lazy_loading_proxies/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Manual_lazy_loading/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Manual_lazy_loading/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Manual_lazy_loading/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Manual_lazy_loading/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/RelationshipCycles/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/RelationshipCycles/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/RelationshipCycles/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/RelationshipCycles/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Self_referential_property/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Self_referential_property/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Self_referential_property/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/Self_referential_property/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs index c224873f6fa..c32d233bbdb 100644 --- a/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs +++ b/test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] diff --git a/test/EFCore.Specification.Tests/Scaffolding/CompiledModelTestBase.cs b/test/EFCore.Specification.Tests/Scaffolding/CompiledModelTestBase.cs index 6db059e5fe1..4131d897023 100644 --- a/test/EFCore.Specification.Tests/Scaffolding/CompiledModelTestBase.cs +++ b/test/EFCore.Specification.Tests/Scaffolding/CompiledModelTestBase.cs @@ -2087,6 +2087,7 @@ protected virtual Task Test( options ??= new CompiledModelCodeGenerationOptions { ForNativeAot = true }; options.ModelNamespace ??= "TestNamespace"; options.ContextType ??= context.GetType(); + options.ProviderName ??= context.GetService().Name; var generator = TestHelpers.CreateDesignServiceProvider( context.GetService().Name, diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/CheckConstraints/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/CheckConstraints/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/CheckConstraints/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/CheckConstraints/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Custom_function_parameter_type_mapping/FunctionParameterTypeMappingContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Custom_function_parameter_type_mapping/FunctionParameterTypeMappingContextAssemblyAttributes.cs index 188db7938f6..370f8aa43ad 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Custom_function_parameter_type_mapping/FunctionParameterTypeMappingContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Custom_function_parameter_type_mapping/FunctionParameterTypeMappingContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.FunctionParameterTypeMappingContext), typeof(FunctionParameterTypeMappingContextModel))] +[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.FunctionParameterTypeMappingContext), typeof(FunctionParameterTypeMappingContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Custom_function_type_mapping/FunctionTypeMappingContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Custom_function_type_mapping/FunctionTypeMappingContextAssemblyAttributes.cs index 7ab16249c50..25d807e86b9 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Custom_function_type_mapping/FunctionTypeMappingContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Custom_function_type_mapping/FunctionTypeMappingContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.FunctionTypeMappingContext), typeof(FunctionTypeMappingContextModel))] +[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.FunctionTypeMappingContext), typeof(FunctionTypeMappingContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/DbFunctions/DbFunctionContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/DbFunctions/DbFunctionContextAssemblyAttributes.cs index a248efefdc0..feed600ccec 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/DbFunctions/DbFunctionContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/DbFunctions/DbFunctionContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.DbFunctionContext), typeof(DbFunctionContextModel))] +[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.DbFunctionContext), typeof(DbFunctionContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Dynamic_schema/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Dynamic_schema/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Dynamic_schema/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Dynamic_schema/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Full_text_index/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Full_text_index/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Full_text_index/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Full_text_index/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Key_HiLo_sequence/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Key_HiLo_sequence/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Key_HiLo_sequence/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Key_HiLo_sequence/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Key_sequence/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Key_sequence/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Key_sequence/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Key_sequence/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Sequences/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Sequences/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Sequences/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Sequences/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/SpatialTypesTest/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/SpatialTypesTest/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/SpatialTypesTest/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/SpatialTypesTest/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Tpc_Sprocs/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Tpc_Sprocs/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Tpc_Sprocs/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Tpc_Sprocs/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Triggers/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Triggers/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Triggers/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Triggers/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Vector_index/DbContextAssemblyAttributes.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Vector_index/DbContextAssemblyAttributes.cs index c224873f6fa..6a0ec176019 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Vector_index/DbContextAssemblyAttributes.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Vector_index/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs index c224873f6fa..43d296d89f6 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextAssemblyAttributes.cs index c224873f6fa..43d296d89f6 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/CheckConstraints/DbContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/CheckConstraints/DbContextAssemblyAttributes.cs index c224873f6fa..43d296d89f6 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/CheckConstraints/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/CheckConstraints/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Custom_function_parameter_type_mapping/FunctionParameterTypeMappingContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Custom_function_parameter_type_mapping/FunctionParameterTypeMappingContextAssemblyAttributes.cs index 188db7938f6..07e942e8bf0 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Custom_function_parameter_type_mapping/FunctionParameterTypeMappingContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Custom_function_parameter_type_mapping/FunctionParameterTypeMappingContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.FunctionParameterTypeMappingContext), typeof(FunctionParameterTypeMappingContextModel))] +[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.FunctionParameterTypeMappingContext), typeof(FunctionParameterTypeMappingContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Custom_function_type_mapping/FunctionTypeMappingContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Custom_function_type_mapping/FunctionTypeMappingContextAssemblyAttributes.cs index 7ab16249c50..991b940cd2a 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Custom_function_type_mapping/FunctionTypeMappingContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Custom_function_type_mapping/FunctionTypeMappingContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.FunctionTypeMappingContext), typeof(FunctionTypeMappingContextModel))] +[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.FunctionTypeMappingContext), typeof(FunctionTypeMappingContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/DbFunctions/DbFunctionContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/DbFunctions/DbFunctionContextAssemblyAttributes.cs index a248efefdc0..353ec8730f2 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/DbFunctions/DbFunctionContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/DbFunctions/DbFunctionContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.DbFunctionContext), typeof(DbFunctionContextModel))] +[assembly: DbContextModel(typeof(CompiledModelRelationalTestBase.DbFunctionContext), typeof(DbFunctionContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Dynamic_schema/DbContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Dynamic_schema/DbContextAssemblyAttributes.cs index c224873f6fa..43d296d89f6 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Dynamic_schema/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Dynamic_schema/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs index c224873f6fa..43d296d89f6 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs index c224873f6fa..43d296d89f6 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/SimpleModel/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Triggers/DbContextAssemblyAttributes.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Triggers/DbContextAssemblyAttributes.cs index c224873f6fa..43d296d89f6 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Triggers/DbContextAssemblyAttributes.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Triggers/DbContextAssemblyAttributes.cs @@ -6,4 +6,4 @@ #pragma warning disable 219, 612, 618 #nullable disable -[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel))] +[assembly: DbContextModel(typeof(DbContext), typeof(DbContextModel), ProviderName = "Microsoft.EntityFrameworkCore.Sqlite")] diff --git a/test/EFCore.Tests/Infrastructure/CompiledModelProviderMismatchTest.cs b/test/EFCore.Tests/Infrastructure/CompiledModelProviderMismatchTest.cs new file mode 100644 index 00000000000..f3a9014d031 --- /dev/null +++ b/test/EFCore.Tests/Infrastructure/CompiledModelProviderMismatchTest.cs @@ -0,0 +1,133 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Diagnostics.Internal; +using Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using Microsoft.EntityFrameworkCore.TestUtilities; + +[assembly: DbContextModel( + typeof(Microsoft.EntityFrameworkCore.Infrastructure.CompiledModelProviderMismatchTest.MismatchedProviderContext), + typeof(Microsoft.EntityFrameworkCore.Infrastructure.CompiledModelProviderMismatchTest.MismatchedProviderTestModel), + ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] + +[assembly: DbContextModel( + typeof(Microsoft.EntityFrameworkCore.Infrastructure.CompiledModelProviderMismatchTest.MultiProviderContext), + typeof(Microsoft.EntityFrameworkCore.Infrastructure.CompiledModelProviderMismatchTest.WrongProviderTestModel), + ProviderName = "Microsoft.EntityFrameworkCore.SqlServer")] +[assembly: DbContextModel( + typeof(Microsoft.EntityFrameworkCore.Infrastructure.CompiledModelProviderMismatchTest.MultiProviderContext), + typeof(Microsoft.EntityFrameworkCore.Infrastructure.CompiledModelProviderMismatchTest.CorrectProviderTestModel), + ProviderName = "Microsoft.EntityFrameworkCore.InMemory")] + +// ReSharper disable InconsistentNaming +namespace Microsoft.EntityFrameworkCore.Infrastructure; + +public class CompiledModelProviderMismatchTest +{ + [ConditionalFact] + public void Compiled_model_with_mismatched_provider_is_skipped_and_warning_is_logged() + { + var serviceProvider = new ServiceCollection() + .AddEntityFrameworkInMemoryDatabase().BuildServiceProvider(validateScopes: true); + + var context = new MismatchedProviderContext(serviceProvider); + var warning = CoreStrings.WarningAsErrorTemplate( + CoreEventId.CompiledModelProviderMismatchWarning, + CoreResources.LogCompiledModelProviderMismatch( + new TestLogger()).GenerateMessage( + "Microsoft.EntityFrameworkCore.SqlServer", "Microsoft.EntityFrameworkCore.InMemory"), + "CoreEventId.CompiledModelProviderMismatchWarning"); + + Assert.Equal( + warning, + Assert.Throws(() => context.Model).Message); + } + + [ConditionalFact] + public void Compiled_model_with_matching_provider_is_used_when_multiple_attributes_exist() + { + var serviceProvider = new ServiceCollection() + .AddEntityFrameworkInMemoryDatabase().BuildServiceProvider(validateScopes: true); + + using var context = new MultiProviderContext(serviceProvider); + var model = context.Model; + + Assert.NotNull(model); + Assert.Same(CorrectProviderTestModel.Instance, model); + } + + public class MismatchedProviderContext(IServiceProvider serviceProvider) : DbContext + { + private readonly IServiceProvider _serviceProvider = serviceProvider; + + protected internal override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder + .UseInternalServiceProvider(_serviceProvider) + .UseInMemoryDatabase(nameof(MismatchedProviderContext)) + .ConfigureWarnings(w => w.Default(WarningBehavior.Throw)); + } + + public class MultiProviderContext(IServiceProvider serviceProvider) : DbContext + { + private readonly IServiceProvider _serviceProvider = serviceProvider; + + protected internal override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder + .UseInternalServiceProvider(_serviceProvider) + .UseInMemoryDatabase(nameof(MultiProviderContext)) + .ConfigureWarnings(w => w.Default(WarningBehavior.Throw)); + } + + public class MismatchedProviderTestModel + { + private static readonly RuntimeModel _instance = CreateModel(); + + public static IModel Instance + => _instance; + + private static RuntimeModel CreateModel() + { +#pragma warning disable CS0618 // Type or member is obsolete + var model = new RuntimeModel(); +#pragma warning restore CS0618 + model.AddAnnotation(CoreAnnotationNames.ProductVersion, ProductInfo.GetVersion()); + return model; + } + } + + public class WrongProviderTestModel + { + private static readonly RuntimeModel _instance = CreateModel(); + + public static IModel Instance + => _instance; + + private static RuntimeModel CreateModel() + { +#pragma warning disable CS0618 // Type or member is obsolete + var model = new RuntimeModel(); +#pragma warning restore CS0618 + model.AddAnnotation(CoreAnnotationNames.ProductVersion, ProductInfo.GetVersion()); + return model; + } + } + + public class CorrectProviderTestModel + { + private static readonly RuntimeModel _instance = CreateModel(); + + public static IModel Instance + => _instance; + + private static RuntimeModel CreateModel() + { +#pragma warning disable CS0618 // Type or member is obsolete + var model = new RuntimeModel(); +#pragma warning restore CS0618 + model.AddAnnotation(CoreAnnotationNames.ProductVersion, ProductInfo.GetVersion()); + return model; + } + } +} diff --git a/test/EFCore.Tests/Infrastructure/ModelSourceTest.cs b/test/EFCore.Tests/Infrastructure/ModelSourceTest.cs index 0011ae8f6a8..3fb099a22d9 100644 --- a/test/EFCore.Tests/Infrastructure/ModelSourceTest.cs +++ b/test/EFCore.Tests/Infrastructure/ModelSourceTest.cs @@ -266,6 +266,37 @@ protected internal override void OnModelCreating(ModelBuilder modelBuilder) private class Context2(DbContextOptions options) : DbContext(options); + [ConditionalFact] + public void Compiled_model_provider_mismatch_warning_has_correct_message() + { + var definition = CoreResources.LogCompiledModelProviderMismatch( + new TestLogger()); + + var message = definition.GenerateMessage("Microsoft.EntityFrameworkCore.SqlServer", "Microsoft.EntityFrameworkCore.InMemory"); + + Assert.Contains("Microsoft.EntityFrameworkCore.SqlServer", message); + Assert.Contains("Microsoft.EntityFrameworkCore.InMemory", message); + } + + [ConditionalFact] + public void DbContextModelAttribute_stores_provider_name() + { + var attr = new DbContextModelAttribute(typeof(DbContext), typeof(object)) + { + ProviderName = "TestProvider" + }; + + Assert.Equal("TestProvider", attr.ProviderName); + } + + [ConditionalFact] + public void DbContextModelAttribute_provider_name_defaults_to_null() + { + var attr = new DbContextModelAttribute(typeof(DbContext), typeof(object)); + + Assert.Null(attr.ProviderName); + } + private class DetailedErrorsContext(IServiceProvider serviceProvider) : DbContext { private readonly IServiceProvider _serviceProvider = serviceProvider;