Skip to content

Commit e62c4b6

Browse files
committedJan 2, 2020
Added EF Core Migrations for multiple database providers, SQLite in development and SQL Server in production
1 parent e24fb98 commit e62c4b6

11 files changed

+182
-10
lines changed
 

‎Helpers/DataContext.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Configuration;
23
using WebApi.Entities;
34

45
namespace WebApi.Helpers
56
{
67
public class DataContext : DbContext
78
{
8-
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
9+
protected readonly IConfiguration Configuration;
10+
11+
public DataContext(IConfiguration configuration)
12+
{
13+
Configuration = configuration;
14+
}
15+
16+
protected override void OnConfiguring(DbContextOptionsBuilder options)
17+
{
18+
// connect to sql server database
19+
options.UseSqlServer(Configuration.GetConnectionString("WebApiDatabase"));
20+
}
921

1022
public DbSet<User> Users { get; set; }
1123
}

‎Helpers/SqliteDataContext.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Configuration;
3+
4+
namespace WebApi.Helpers
5+
{
6+
public class SqliteDataContext : DataContext
7+
{
8+
public SqliteDataContext(IConfiguration configuration) : base(configuration) { }
9+
10+
protected override void OnConfiguring(DbContextOptionsBuilder options)
11+
{
12+
// connect to sqlite database
13+
options.UseSqlite(Configuration.GetConnectionString("WebApiDatabase"));
14+
}
15+
}
16+
}

‎Migrations/SqlServerMigrations/20200102103423_InitialCreate.Designer.cs

+53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
namespace WebApi.Migrations.SqlServerMigrations
5+
{
6+
public partial class InitialCreate : Migration
7+
{
8+
protected override void Up(MigrationBuilder migrationBuilder)
9+
{
10+
migrationBuilder.CreateTable(
11+
name: "Users",
12+
columns: table => new
13+
{
14+
Id = table.Column<int>(nullable: false)
15+
.Annotation("SqlServer:Identity", "1, 1"),
16+
FirstName = table.Column<string>(nullable: true),
17+
LastName = table.Column<string>(nullable: true),
18+
Username = table.Column<string>(nullable: true),
19+
PasswordHash = table.Column<byte[]>(nullable: true),
20+
PasswordSalt = table.Column<byte[]>(nullable: true)
21+
},
22+
constraints: table =>
23+
{
24+
table.PrimaryKey("PK_Users", x => x.Id);
25+
});
26+
}
27+
28+
protected override void Down(MigrationBuilder migrationBuilder)
29+
{
30+
migrationBuilder.DropTable(
31+
name: "Users");
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// <auto-generated />
2+
using System;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Metadata;
6+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
7+
using WebApi.Helpers;
8+
9+
namespace WebApi.Migrations.SqlServerMigrations
10+
{
11+
[DbContext(typeof(DataContext))]
12+
partial class DataContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
#pragma warning disable 612, 618
17+
modelBuilder
18+
.HasAnnotation("ProductVersion", "3.1.0")
19+
.HasAnnotation("Relational:MaxIdentifierLength", 128)
20+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
21+
22+
modelBuilder.Entity("WebApi.Entities.User", b =>
23+
{
24+
b.Property<int>("Id")
25+
.ValueGeneratedOnAdd()
26+
.HasColumnType("int")
27+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
28+
29+
b.Property<string>("FirstName")
30+
.HasColumnType("nvarchar(max)");
31+
32+
b.Property<string>("LastName")
33+
.HasColumnType("nvarchar(max)");
34+
35+
b.Property<byte[]>("PasswordHash")
36+
.HasColumnType("varbinary(max)");
37+
38+
b.Property<byte[]>("PasswordSalt")
39+
.HasColumnType("varbinary(max)");
40+
41+
b.Property<string>("Username")
42+
.HasColumnType("nvarchar(max)");
43+
44+
b.HasKey("Id");
45+
46+
b.ToTable("Users");
47+
});
48+
#pragma warning restore 612, 618
49+
}
50+
}
51+
}

‎Migrations/20191223052218_InitialCreate.Designer.cs ‎Migrations/SqliteMigrations/20200102102942_InitialCreate.Designer.cs

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Migrations/20191223052218_InitialCreate.cs ‎Migrations/SqliteMigrations/20200102102942_InitialCreate.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Microsoft.EntityFrameworkCore.Migrations;
33

4-
namespace WebApi.Migrations
4+
namespace WebApi.Migrations.SqliteMigrations
55
{
66
public partial class InitialCreate : Migration
77
{

‎Migrations/DataContextModelSnapshot.cs ‎Migrations/SqliteMigrations/SqliteDataContextModelSnapshot.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
66
using WebApi.Helpers;
77

8-
namespace WebApi.Migrations
8+
namespace WebApi.Migrations.SqliteMigrations
99
{
10-
[DbContext(typeof(DataContext))]
11-
partial class DataContextModelSnapshot : ModelSnapshot
10+
[DbContext(typeof(SqliteDataContext))]
11+
partial class SqliteDataContextModelSnapshot : ModelSnapshot
1212
{
1313
protected override void BuildModel(ModelBuilder modelBuilder)
1414
{

‎Startup.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public void ConfigureServices(IServiceCollection services)
3131
{
3232
// use sql server db in production and sqlite db in development
3333
if (_env.IsProduction())
34-
services.AddDbContext<DataContext>(x => x.UseSqlServer(_configuration.GetConnectionString("WebApiDatabase")));
34+
services.AddDbContext<DataContext>();
3535
else
36-
services.AddDbContext<DataContext>(x => x.UseSqlite("Data Source=LocalDatabase.db"));
36+
services.AddDbContext<DataContext, SqliteDataContext>();
3737

3838
services.AddCors();
3939
services.AddControllers();

‎appsettings.Development.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"ConnectionStrings": {
3+
"WebApiDatabase": "Data Source=LocalDatabase.db"
4+
},
25
"Logging": {
36
"LogLevel": {
47
"Default": "Debug",

‎appsettings.json

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"AppSettings": {
33
"Secret": "THIS IS USED TO SIGN AND VERIFY JWT TOKENS, REPLACE IT WITH YOUR OWN SECRET, IT CAN BE ANY STRING"
44
},
5+
"ConnectionStrings": {
6+
"WebApiDatabase": "ENTER PRODUCTION SQL SERVER CONNECTION STRING HERE"
7+
},
58
"Logging": {
69
"LogLevel": {
710
"Default": "Information",

0 commit comments

Comments
 (0)
Please sign in to comment.