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 virtual void Optimize(string?outputDir, string?modelNamespace, string?contextType)
        {
            using var context = CreateContext(contextType);

            var services   = _servicesBuilder.Build(context);
            var scaffolder = services.GetRequiredService <ICompiledModelScaffolder>();

            outputDir = outputDir != null
                ? Path.GetFullPath(Path.Combine(_projectDir, outputDir))
                : _projectDir;

            var finalModelNamespace = modelNamespace ?? GetNamespaceFromOutputPath(outputDir) ?? "";

            scaffolder.ScaffoldModel(
                context.GetService <IDesignTimeModel>().Model,
                outputDir,
                new CompiledModelCodeGenerationOptions
            {
                ContextType               = context.GetType(),
                ModelNamespace            = finalModelNamespace,
                Language                  = _language,
                UseNullableReferenceTypes = _nullable
            });

            var fullName = context.GetType().ShortDisplayName() + "Model";

            if (!string.IsNullOrEmpty(modelNamespace))
            {
                fullName = modelNamespace + "." + fullName;
            }

            _reporter.WriteInformation(DesignStrings.CompiledModelGenerated($"options.UseModel({fullName}.Instance)"));

            var cacheKeyFactory = context.GetService <IModelCacheKeyFactory>();

            if (!(cacheKeyFactory is ModelCacheKeyFactory))
            {
                _reporter.WriteWarning(DesignStrings.CompiledModelCustomCacheKeyFactory(cacheKeyFactory.GetType().ShortDisplayName()));
            }
        }