Created
March 27, 2020 04:58
-
-
Save danielplawgo/d4eec7b15ad1d30742d51ad2432e1a6e to your computer and use it in GitHub Desktop.
EF Core uruchamianie migracji w Azure DevOps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- task: DotNetCoreCLI@2 | |
displayName: "Migrations Publish" | |
inputs: | |
command: 'publish' | |
publishWebProjects: false | |
projects: 'EFCoreMigrations.Migrator/EFCoreMigrations.Migrator.csproj' | |
arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)/Migrations --self-contained true -r win10-x64' | |
zipAfterPublish: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ItemGroup> | |
<ProjectReference Include="..\EFCoreMigrations.Web\EFCoreMigrations.Web.csproj" GlobalPropertiesToRemove="SelfContained" /> | |
</ItemGroup> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error NETSDK1031: It is not supported to build or publish a self-contained application without specifying a RuntimeIdentifier. | |
Please either specify a RuntimeIdentifier or set SelfContained to false. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static ServiceProvider GetServiceCollection(Options options) | |
{ | |
var serviceCollection = new ServiceCollection(); | |
var config = new ConfigurationBuilder() | |
.AddInMemoryCollection(new Dictionary<string, string>() | |
{ | |
{"ConnectionStrings:DefaultConnection", options.ConnectionString} | |
}) | |
.Build(); | |
serviceCollection.AddSingleton<IConfiguration>(config); | |
var startup = new Startup(config); | |
startup.ConfigureServices(serviceCollection); | |
return serviceCollection.BuildServiceProvider(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void Migrate(Options options) | |
{ | |
var serviceProvider = GetServiceCollection(options); | |
var context = serviceProvider.GetRequiredService<DataContext>(); | |
Console.WriteLine("Migration is in progress..."); | |
Console.WriteLine("All:"); | |
Console.WriteLine(FormatMigrations(context.Database.GetMigrations())); | |
Console.WriteLine("Applied:"); | |
Console.WriteLine(FormatMigrations(context.Database.GetAppliedMigrations())); | |
Console.WriteLine("Pending:"); | |
Console.WriteLine(FormatMigrations(context.Database.GetPendingMigrations())); | |
Console.WriteLine("Migrating..."); | |
context.Database.Migrate(); | |
Console.WriteLine("Database has been migrated"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Options | |
{ | |
[Option('c', "connectionString", Required = true, HelpText = "The connection string to database that needs to be updated.")] | |
public string ConnectionString { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var result = Parser.Default.ParseArguments<Options>(args); | |
result | |
.WithParsed(Migrate); | |
} | |
private static void Migrate(Options options) | |
{ | |
var serviceProvider = GetServiceCollection(options); | |
var context = serviceProvider.GetRequiredService<DataContext>(); | |
Console.WriteLine("Migration is in progress..."); | |
Console.WriteLine("All:"); | |
Console.WriteLine(FormatMigrations(context.Database.GetMigrations())); | |
Console.WriteLine("Applied:"); | |
Console.WriteLine(FormatMigrations(context.Database.GetAppliedMigrations())); | |
Console.WriteLine("Pending:"); | |
Console.WriteLine(FormatMigrations(context.Database.GetPendingMigrations())); | |
Console.WriteLine("Migrating..."); | |
context.Database.Migrate(); | |
Console.WriteLine("Database has been migrated"); | |
} | |
private static string FormatMigrations(IEnumerable<string> migrations) | |
{ | |
if (migrations.Any() == false) | |
{ | |
return "\tNone"; | |
} | |
return string.Join(Environment.NewLine, migrations.Select(m => $"\t{m}")); | |
} | |
private static ServiceProvider GetServiceCollection(Options options) | |
{ | |
var serviceCollection = new ServiceCollection(); | |
var config = new ConfigurationBuilder() | |
.AddInMemoryCollection(new Dictionary<string, string>() | |
{ | |
{"ConnectionStrings:DefaultConnection", options.ConnectionString} | |
}) | |
.Build(); | |
serviceCollection.AddSingleton<IConfiguration>(config); | |
var startup = new Startup(config); | |
startup.ConfigureServices(serviceCollection); | |
return serviceCollection.BuildServiceProvider(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment