Skip to content

Commit

Permalink
final migration
Browse files Browse the repository at this point in the history
  • Loading branch information
BohdanTron committed Oct 5, 2022
1 parent 279d8c0 commit 762cdbc
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
12 changes: 12 additions & 0 deletions WebApi/DataAccess/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,17 @@ public class AppDbContext : DbContext
public AppDbContext(DbContextOptions options) : base(options) { }

public DbSet<User> Users => Set<User>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

// Seed initial data
modelBuilder.Entity<User>().HasData(
new User { Id = 1, Name = "James" },
new User { Id = 2, Name = "Lars" },
new User { Id = 3, Name = "Kirk" },
new User { Id = 4, Name = "Cliff" });
}
}
}
60 changes: 60 additions & 0 deletions WebApi/Migrations/20221005175241_InitialData.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions WebApi/Migrations/20221005175241_InitialData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace WebApi.Migrations
{
public partial class InitialData : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.InsertData(
table: "Users",
columns: new[] { "Id", "Name" },
values: new object[] { 1, "James" });

migrationBuilder.InsertData(
table: "Users",
columns: new[] { "Id", "Name" },
values: new object[] { 2, "Lars" });

migrationBuilder.InsertData(
table: "Users",
columns: new[] { "Id", "Name" },
values: new object[] { 3, "Kirk" });

migrationBuilder.InsertData(
table: "Users",
columns: new[] { "Id", "Name" },
values: new object[] { 4, "Cliff" });
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DeleteData(
table: "Users",
keyColumn: "Id",
keyValue: 1);

migrationBuilder.DeleteData(
table: "Users",
keyColumn: "Id",
keyValue: 2);

migrationBuilder.DeleteData(
table: "Users",
keyColumn: "Id",
keyValue: 3);

migrationBuilder.DeleteData(
table: "Users",
keyColumn: "Id",
keyValue: 4);
}
}
}
22 changes: 22 additions & 0 deletions WebApi/Migrations/AppDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasKey("Id");

b.ToTable("Users");

b.HasData(
new
{
Id = 1,
Name = "James"
},
new
{
Id = 2,
Name = "Lars"
},
new
{
Id = 3,
Name = "Kirk"
},
new
{
Id = 4,
Name = "Cliff"
});
});
#pragma warning restore 612, 618
}
Expand Down
7 changes: 7 additions & 0 deletions WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@
app.UseSwagger();
app.UseSwaggerUI();

app.MapGet("/users", (CancellationToken cancellationToken) =>
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
return dbContext.Users.ToListAsync(cancellationToken);
});

app.Run();
Binary file modified WebApi/SQLite.db
Binary file not shown.
Binary file added WebApi/SQLite.db-shm
Binary file not shown.
Empty file added WebApi/SQLite.db-wal
Empty file.

0 comments on commit 762cdbc

Please sign in to comment.