Skip to content

Commit 8f400e6

Browse files
committed
add db context and db
1 parent 4de6c94 commit 8f400e6

8 files changed

+156
-0
lines changed

API/Data/Migrations/20240823185906_InitialCreate.Designer.cs

+46
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,36 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace API.Data.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class InitialCreate : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.CreateTable(
14+
name: "User",
15+
columns: table => new
16+
{
17+
Id = table.Column<int>(type: "INTEGER", nullable: false)
18+
.Annotation("Sqlite:Autoincrement", true),
19+
FirstName = table.Column<string>(type: "TEXT", nullable: false),
20+
LastName = table.Column<string>(type: "TEXT", nullable: false),
21+
Email = table.Column<string>(type: "TEXT", nullable: true)
22+
},
23+
constraints: table =>
24+
{
25+
table.PrimaryKey("PK_User", x => x.Id);
26+
});
27+
}
28+
29+
/// <inheritdoc />
30+
protected override void Down(MigrationBuilder migrationBuilder)
31+
{
32+
migrationBuilder.DropTable(
33+
name: "User");
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// <auto-generated />
2+
using API.Data;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
6+
7+
#nullable disable
8+
9+
namespace API.Data.Migrations
10+
{
11+
[DbContext(typeof(TestContext))]
12+
partial class TestContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
#pragma warning disable 612, 618
17+
modelBuilder.HasAnnotation("ProductVersion", "7.0.7");
18+
19+
modelBuilder.Entity("API.Entities.User", b =>
20+
{
21+
b.Property<int>("Id")
22+
.ValueGeneratedOnAdd()
23+
.HasColumnType("INTEGER");
24+
25+
b.Property<string>("Email")
26+
.HasColumnType("TEXT");
27+
28+
b.Property<string>("FirstName")
29+
.IsRequired()
30+
.HasColumnType("TEXT");
31+
32+
b.Property<string>("LastName")
33+
.IsRequired()
34+
.HasColumnType("TEXT");
35+
36+
b.HasKey("Id");
37+
38+
b.ToTable("User");
39+
});
40+
#pragma warning restore 612, 618
41+
}
42+
}
43+
}

API/Data/TestContext.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using API.Entities;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace API.Data
5+
{
6+
public class TestContext : DbContext
7+
{
8+
public TestContext(DbContextOptions options) : base(options) {}
9+
10+
public DbSet<User> User { get; set; }
11+
}
12+
}

API/Entities/User.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace API.Entities
2+
{
3+
public class User
4+
{
5+
public required int Id { get; set; }
6+
public required string FirstName { get; set; }
7+
public required string LastName { get; set; }
8+
public string? Email { get; set; }
9+
}
10+
}

API/Program.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using API.Data;
2+
using Microsoft.EntityFrameworkCore;
3+
14
var builder = WebApplication.CreateBuilder(args);
25

36
// Add services to the container.
@@ -6,6 +9,9 @@
69
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
710
builder.Services.AddEndpointsApiExplorer();
811
builder.Services.AddSwaggerGen();
12+
builder.Services.AddDbContext<TestContext>(options => {
13+
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"));
14+
});
915

1016
var app = builder.Build();
1117

API/appsettings.Development.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"Default": "Information",
55
"Microsoft.AspNetCore": "Warning"
66
}
7+
},
8+
"ConnectionStrings": {
9+
"DefaultConnection": "Data source=test.db"
710
}
811
}

API/test.db

20 KB
Binary file not shown.

0 commit comments

Comments
 (0)