Skip to content

Commit d540846

Browse files
committed
Updated from EF Core InMemory Database to SQLite Database
1 parent 07d0a4e commit d540846

7 files changed

+171
-13
lines changed

Diff for: .gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ typings
3939

4040
# .NET compiled files
4141
bin
42-
obj
42+
obj
43+
44+
# local sqlite db that is auto generated
45+
LocalDatabase.db

Diff for: Migrations/20191223052218_InitialCreate.Designer.cs

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

Diff for: Migrations/20191223052218_InitialCreate.cs

+34
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
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("Sqlite:Autoincrement", true),
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+
}

Diff for: Migrations/DataContextModelSnapshot.cs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// <auto-generated />
2+
using System;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
6+
using WebApi.Helpers;
7+
8+
namespace WebApi.Migrations
9+
{
10+
[DbContext(typeof(DataContext))]
11+
partial class DataContextModelSnapshot : ModelSnapshot
12+
{
13+
protected override void BuildModel(ModelBuilder modelBuilder)
14+
{
15+
#pragma warning disable 612, 618
16+
modelBuilder
17+
.HasAnnotation("ProductVersion", "3.1.0");
18+
19+
modelBuilder.Entity("WebApi.Entities.User", b =>
20+
{
21+
b.Property<int>("Id")
22+
.ValueGeneratedOnAdd()
23+
.HasColumnType("INTEGER");
24+
25+
b.Property<string>("FirstName")
26+
.HasColumnType("TEXT");
27+
28+
b.Property<string>("LastName")
29+
.HasColumnType("TEXT");
30+
31+
b.Property<byte[]>("PasswordHash")
32+
.HasColumnType("BLOB");
33+
34+
b.Property<byte[]>("PasswordSalt")
35+
.HasColumnType("BLOB");
36+
37+
b.Property<string>("Username")
38+
.HasColumnType("TEXT");
39+
40+
b.HasKey("Id");
41+
42+
b.ToTable("Users");
43+
});
44+
#pragma warning restore 612, 618
45+
}
46+
}
47+
}

Diff for: Properties/launchSettings.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"Development": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"ASPNETCORE_ENVIRONMENT": "Development"
7+
}
8+
}
9+
}
10+
}

Diff for: Startup.cs

+21-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.EntityFrameworkCore;
7+
using Microsoft.Extensions.Hosting;
78
using WebApi.Helpers;
89
using WebApi.Services;
910
using AutoMapper;
@@ -14,25 +15,32 @@
1415

1516
namespace WebApi
1617
{
17-
public class Startup
18+
public class Startup
1819
{
19-
public Startup(IConfiguration configuration)
20+
private readonly IWebHostEnvironment _env;
21+
private readonly IConfiguration _configuration;
22+
23+
public Startup(IWebHostEnvironment env, IConfiguration configuration)
2024
{
21-
Configuration = configuration;
25+
_env = env;
26+
_configuration = configuration;
2227
}
2328

24-
public IConfiguration Configuration { get; }
25-
2629
// This method gets called by the runtime. Use this method to add services to the container.
2730
public void ConfigureServices(IServiceCollection services)
2831
{
32+
// use sql server db in production and sqlite db in development
33+
if (_env.IsProduction())
34+
services.AddDbContext<DataContext>(x => x.UseSqlServer(_configuration.GetConnectionString("WebApiDatabase")));
35+
else
36+
services.AddDbContext<DataContext>(x => x.UseSqlite("Data Source=LocalDatabase.db"));
37+
2938
services.AddCors();
30-
services.AddDbContext<DataContext>(x => x.UseInMemoryDatabase("TestDb"));
3139
services.AddControllers();
3240
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
3341

3442
// configure strongly typed settings objects
35-
var appSettingsSection = Configuration.GetSection("AppSettings");
43+
var appSettingsSection = _configuration.GetSection("AppSettings");
3644
services.Configure<AppSettings>(appSettingsSection);
3745

3846
// configure jwt authentication
@@ -78,6 +86,10 @@ public void ConfigureServices(IServiceCollection services)
7886
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
7987
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
8088
{
89+
// migrate any database changes on startup (includes initial db creation)
90+
using (var scope = app.ApplicationServices.CreateScope())
91+
scope.ServiceProvider.GetService<DataContext>().Database.Migrate();
92+
8193
app.UseRouting();
8294

8395
// global cors policy
@@ -88,10 +100,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
88100

89101
app.UseAuthentication();
90102
app.UseAuthorization();
91-
92-
app.UseEndpoints(endpoints => {
93-
endpoints.MapControllers();
94-
});
103+
104+
app.UseEndpoints(endpoints => endpoints.MapControllers());
95105
}
96106
}
97107
}

Diff for: WebApi.csproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
88
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.0" />
99
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
10-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12+
<PrivateAssets>all</PrivateAssets>
13+
</PackageReference>
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
1116
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
1217
</ItemGroup>
1318
</Project>

0 commit comments

Comments
 (0)