diff --git a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/DesignTimeDbContextFactoryBase.cs b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/DesignTimeDbContextFactoryBase.cs new file mode 100644 index 000000000..16f9e7629 --- /dev/null +++ b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/DesignTimeDbContextFactoryBase.cs @@ -0,0 +1,29 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; + +namespace Elsa.Persistence.EntityFrameworkCore.DbContexts +{ + public abstract class DesignTimeDbContextFactoryBase : IDesignTimeDbContextFactory where TDbContext : DbContext + { + public TDbContext CreateDbContext(string[] args) + { + var optionsBuilder = new DbContextOptionsBuilder(); + var migrationAssembly = typeof(TDbContext).Assembly.FullName; + var connectionString = Environment.GetEnvironmentVariable("EF_CONNECTIONSTRING"); + + if(connectionString == null) + { + var providerName = typeof(TDbContext).Name.Replace("Context", ""); + throw new InvalidOperationException($"Set the EF_CONNECTIONSTRING environment variable to a valid {providerName} connection string."); + } + + optionsBuilder.UseSqlite( + connectionString, + x => x.MigrationsAssembly(migrationAssembly) + ); + + return (TDbContext)Activator.CreateInstance(typeof(TDbContext), optionsBuilder.Options); + } + } +} \ No newline at end of file diff --git a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/MySqlContextFactory.cs b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/MySqlContextFactory.cs index 9ccede4dd..e47565d52 100644 --- a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/MySqlContextFactory.cs +++ b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/MySqlContextFactory.cs @@ -1,26 +1,6 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Design; - namespace Elsa.Persistence.EntityFrameworkCore.DbContexts { - public class MySqlContextFactory : IDesignTimeDbContextFactory + public class MySqlContextFactory : DesignTimeDbContextFactoryBase { - public MySqlContext CreateDbContext(string[] args) - { - var optionsBuilder = new DbContextOptionsBuilder(); - var migrationAssembly = typeof(MySqlContext).Assembly.FullName; - var connectionString = Environment.GetEnvironmentVariable("EF_CONNECTIONSTRING"); - - if(connectionString == null) - throw new InvalidOperationException("Set the EF_CONNECTIONSTRING environment variable to a valid MySQL connection string. E.g. SET EF_CONNECTIONSTRING=Server=localhost;Database=Elsa;User=sa;Password=Secret_password123!;"); - - optionsBuilder.UseMySql( - connectionString, - x => x.MigrationsAssembly(migrationAssembly) - ); - - return new MySqlContext(optionsBuilder.Options); - } } } \ No newline at end of file diff --git a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/PostgreSqlContextFactory.cs b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/PostgreSqlContextFactory.cs index a9061655c..d8ce8f01a 100644 --- a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/PostgreSqlContextFactory.cs +++ b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/PostgreSqlContextFactory.cs @@ -1,26 +1,6 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Design; -using System; - namespace Elsa.Persistence.EntityFrameworkCore.DbContexts { - public class PostgreSqlContextFactory : IDesignTimeDbContextFactory + public class PostgreSqlContextFactory : DesignTimeDbContextFactoryBase { - public PostgreSqlContext CreateDbContext(string[] args) - { - var optionsBuilder = new DbContextOptionsBuilder(); - var migrationAssembly = typeof(ElsaContext).Assembly.FullName; - var connectionString = Environment.GetEnvironmentVariable("EF_CONNECTIONSTRING"); - - if (connectionString == null) - throw new InvalidOperationException(@"Set the EF_CONNECTIONSTRING environment variable to a valid PostgreSql connection string. E.g. SET EF_CONNECTIONSTRING=Server=localhost;Database=Elsa;Port=5432;User Id=postgres;Password=Secret_password123!;"); - - optionsBuilder.UseNpgsql( - connectionString, - x => x.MigrationsAssembly(migrationAssembly) - ); - - return new PostgreSqlContext(optionsBuilder.Options); - } } } \ No newline at end of file diff --git a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqlServerContextFactory.cs b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqlServerContextFactory.cs index e7eb5813d..df70f324d 100644 --- a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqlServerContextFactory.cs +++ b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqlServerContextFactory.cs @@ -1,26 +1,6 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Design; - namespace Elsa.Persistence.EntityFrameworkCore.DbContexts { - public class SqlServerContextFactory : IDesignTimeDbContextFactory + public class SqlServerContextFactory : DesignTimeDbContextFactoryBase { - public SqlServerContext CreateDbContext(string[] args) - { - var optionsBuilder = new DbContextOptionsBuilder(); - var migrationAssembly = typeof(ElsaContext).Assembly.FullName; - var connectionString = Environment.GetEnvironmentVariable("EF_CONNECTIONSTRING"); - - if(connectionString == null) - throw new InvalidOperationException("Set the EF_CONNECTIONSTRING environment variable to a valid SQL Server connection string. E.g. SET EF_CONNECTIONSTRING=Server=localhost;Database=Elsa;User=sa;Password=Secret_password123!;"); - - optionsBuilder.UseSqlServer( - connectionString, - x => x.MigrationsAssembly(migrationAssembly) - ); - - return new SqlServerContext(optionsBuilder.Options); - } } } \ No newline at end of file diff --git a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqliteContext.cs b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqliteContext.cs index 96db59220..2dea096c3 100644 --- a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqliteContext.cs +++ b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqliteContext.cs @@ -4,7 +4,7 @@ namespace Elsa.Persistence.EntityFrameworkCore.DbContexts { public class SqliteContext : ElsaContext { - public SqliteContext(DbContextOptions options) : base(options) + public SqliteContext(DbContextOptions options) : base(options) { } } diff --git a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqliteContextFactory.cs b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqliteContextFactory.cs index 548ec7f5f..4c2c94591 100644 --- a/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqliteContextFactory.cs +++ b/src/persistence/Elsa.Persistence.EntityFrameworkCore/DbContexts/SqliteContextFactory.cs @@ -1,26 +1,6 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Design; - namespace Elsa.Persistence.EntityFrameworkCore.DbContexts { - public class SqliteContextFactory : IDesignTimeDbContextFactory + public class SqliteContextFactory : DesignTimeDbContextFactoryBase { - public SqliteContext CreateDbContext(string[] args) - { - var optionsBuilder = new DbContextOptionsBuilder(); - var migrationAssembly = typeof(ElsaContext).Assembly.FullName; - var connectionString = Environment.GetEnvironmentVariable("EF_CONNECTIONSTRING"); - - if(connectionString == null) - throw new InvalidOperationException(@"Set the EF_CONNECTIONSTRING environment variable to a valid SQLite connection string. E.g. SET EF_CONNECTIONSTRING=Data Source=c:\data\elsa.db;Cache=Shared;"); - - optionsBuilder.UseSqlite( - connectionString, - x => x.MigrationsAssembly(migrationAssembly) - ); - - return new SqliteContext(optionsBuilder.Options); - } } } \ No newline at end of file diff --git a/src/persistence/Elsa.Persistence.EntityFrameworkCore/Elsa.Persistence.EntityFrameworkCore.csproj b/src/persistence/Elsa.Persistence.EntityFrameworkCore/Elsa.Persistence.EntityFrameworkCore.csproj index d85f10b45..6b34d8e9a 100644 --- a/src/persistence/Elsa.Persistence.EntityFrameworkCore/Elsa.Persistence.EntityFrameworkCore.csproj +++ b/src/persistence/Elsa.Persistence.EntityFrameworkCore/Elsa.Persistence.EntityFrameworkCore.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 diff --git a/src/samples/Sample16/Sample16.csproj b/src/samples/Sample16/Sample16.csproj index c115b3f18..f5c96348e 100644 --- a/src/samples/Sample16/Sample16.csproj +++ b/src/samples/Sample16/Sample16.csproj @@ -1,7 +1,7 @@  - netcoreapp3.0 + netcoreapp3.1 diff --git a/src/samples/Sample19/Sample19.csproj b/src/samples/Sample19/Sample19.csproj index 04be26654..49471d2fe 100644 --- a/src/samples/Sample19/Sample19.csproj +++ b/src/samples/Sample19/Sample19.csproj @@ -1,7 +1,7 @@  - netcoreapp3.0 + netcoreapp3.1 diff --git a/src/samples/Sample23/Sample23.csproj b/src/samples/Sample23/Sample23.csproj index 338ac71c1..bf7f8b6d6 100644 --- a/src/samples/Sample23/Sample23.csproj +++ b/src/samples/Sample23/Sample23.csproj @@ -6,12 +6,12 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - +