Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent to ObjectContext.CreateDatabaseScript #2943

Closed
bricelam opened this issue Aug 27, 2015 · 7 comments
Closed

Equivalent to ObjectContext.CreateDatabaseScript #2943

bricelam opened this issue Aug 27, 2015 · 7 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-enhancement
Milestone

Comments

@bricelam
Copy link
Contributor

bricelam commented Aug 27, 2015

This is one of my favorite features of the EF Power Tools, and I think we should have similar functionality in EF Core.

My proposal is an db.Database.GenerateCreateScript() extension method. Here's the implementation. 😉

public static string GenerateCreateScript(this DatabaseFacade database)
{
    var model = database.GetService<IModel>();
    var differ = database.GetService<IMigrationsModelDiffer>();
    var generator = database.GetService<IMigrationsSqlGenerator>();
    var sql = database.GetService<ISqlGenerationHelper>();

    var operations = differ.GetDifferences(null, model);
    var commands = generator.Generate(operations, model);

    var builder = new StringBuilder();
    foreach (var command in commands)
    {
        builder
            .Append(command.CommandText)
            .AppendLine(sql.BatchTerminator);
    }

    return builder.ToString();
}
@rowanmiller rowanmiller added this to the Backlog milestone Aug 28, 2015
@bricelam
Copy link
Contributor Author

bricelam commented Aug 11, 2016

Note to implementer: Common logic should be shared with RelationalDatabaseCreator and Migrator.

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 11, 2017

I will try to get this added to EF Core Power Tools ErikEJ/SqlCeToolbox#521

@bricelam
Copy link
Contributor Author

@ErikEJ, we'd also accept a PR here too. It feels like a generally useful thing to have in Relational.

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 16, 2017

@bricelam Considering a PR for this - you wrote:

Common logic should be shared with RelationalDatabaseCreator and Migrator.

What "common logic" are you referring to? Or is that comment obsolete?

@bricelam
Copy link
Contributor Author

  1. The logic to get the current model and turn it into commands (in RelationalDatabaseCrator)
  2. The logic of combining commands with a BatchTerminator (in Migrator)

I imagine the extension method on DatabaseFacade will call an implementation like RelationalDatabaseCreator.GenerateCreateScript().

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 17, 2017

@bricelam Thanks, all I can see in Migrator is this private, unused (?) method:
https://github.com/aspnet/EntityFrameworkCore/blob/dev/src/EFCore.Relational/Migrations/Internal/Migrator.cs#L360

@bricelam
Copy link
Contributor Author

I suppose I was thinking the GenerateScript method, but it doesn't look like much can be reused there.

ErikEJ added a commit to ErikEJ/EntityFramework that referenced this issue Oct 22, 2017
bricelam pushed a commit that referenced this issue Nov 22, 2017
Add GenerateCreateScript extension method on DatabaseFacade

fixes #2943
@bricelam bricelam modified the milestones: Backlog, 2.1.0 Nov 22, 2017
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 22, 2017
@bricelam bricelam added the good first issue This issue should be relatively straightforward to fix. label May 31, 2019
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-enhancement
Projects
None yet
Development

No branches or pull requests

4 participants