Esempio n. 1
0
        public List <Tuple <string, string> > GenerateDebugView(string outputPath)
        {
            var result = new List <Tuple <string, string> >();

            var assembly = Load(outputPath);

            if (assembly == null)
            {
                throw new ArgumentException("Unable to load project assembly");
            }

            var reporter = new OperationReporter(
                new OperationReportHandler());

            var operations = new DbContextOperations(reporter, assembly, assembly);
            var types      = operations.GetContextTypes().ToList();

            if (types.Count == 0)
            {
                throw new ArgumentException("No DbContext types found in the project");
            }

            foreach (var type in types)
            {
                var dbContext = operations.CreateContext(types[0].Name);
                var debugView = dbContext.Model.AsModel().DebugView.View;
                result.Add(new Tuple <string, string>(type.Name, debugView));
            }

            return(result);
        }
Esempio n. 2
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 MigrationFiles AddMigration(
        string name,
        string?outputDir,
        string?contextType,
        string? @namespace)
    {
        if (outputDir != null)
        {
            outputDir = Path.GetFullPath(Path.Combine(_projectDir, outputDir));
        }

        var subNamespace = SubnamespaceFromOutputPath(outputDir);

        using var context = _contextOperations.CreateContext(contextType);
        var contextClassName = context.GetType().Name;

        if (string.Equals(name, contextClassName, StringComparison.Ordinal))
        {
            throw new OperationException(
                      DesignStrings.ConflictingContextAndMigrationName(name));
        }

        var services = _servicesBuilder.Build(context);

        EnsureServices(services);
        EnsureMigrationsAssembly(services);

        using var scope = services.CreateScope();
        var scaffolder = scope.ServiceProvider.GetRequiredService <IMigrationsScaffolder>();
        var migration  =
            string.IsNullOrEmpty(@namespace)
            // TODO: Honor _nullable (issue #18950)
                ? scaffolder.ScaffoldMigration(name, _rootNamespace ?? string.Empty, subNamespace, _language)
                : scaffolder.ScaffoldMigration(name, null, @namespace, _language);

        return(scaffolder.Save(_projectDir, migration, outputDir));
    }
Esempio n. 3
0
        private DbContext TryCreateContextUsingAppCode(Type dbContextType, Type startupType)
        {
            try
            {
                // Use EF design APIs to get the DBContext instance.
                var operationHandler  = new OperationReportHandler();
                var operationReporter = new OperationReporter(operationHandler);
                // EF infers the environment (Development/ Production) based on the environment variable
                // ASPNETCORE_ENVIRONMENT. This should already be set up by the CodeGeneration.Design process.
                var dbContextOperations = new DbContextOperations(
                    operationReporter,
                    dbContextType.GetTypeInfo().Assembly,
                    startupType.GetTypeInfo().Assembly);

                var dbContextService = dbContextOperations.CreateContext(dbContextType.FullName);

                return(dbContextService);
            }
            catch (Exception ex)
            {
                throw ex.Unwrap(_logger);
            }
        }