Skip to content

Commit

Permalink
Added DbContext and Migrations Management Script (#4)
Browse files Browse the repository at this point in the history
Fix #3
  • Loading branch information
Hutch79 authored Jul 3, 2024
1 parent 855b583 commit 2d0606b
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 2 deletions.
10 changes: 10 additions & 0 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Your_Lab</RootNamespace>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<UserSecretsId>456c6c86-04fa-4b03-b088-81fcf2fb5760</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>

Expand All @@ -18,4 +24,8 @@
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Infrastructure;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -7,8 +10,20 @@
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<YourLabDbContext>(context =>
{
context.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
var connectionString = builder.Configuration.GetConnectionString("SqlConnectionString");
context.UseSqlServer(connectionString);
});

var app = builder.Build();

using var scope = app.Services.CreateScope();

var context = scope.ServiceProvider.GetRequiredService<YourLabDbContext>();
context.Database.Migrate();

// Configure the HTTP request pipeline.
// if (app.Environment.IsDevelopment())
// {
Expand All @@ -18,6 +33,7 @@

app.UseHttpsRedirection();

// app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
Expand Down
4 changes: 4 additions & 0 deletions Domain/Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Domain/Domains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Domain;

public class Domains
{
public int Id { get; set; }
public required string Domain { get; set; }
public required bool Active { get; set; }
}
1 change: 1 addition & 0 deletions Domain/Subdomains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Domain;

public class Subdomains
{
public int Id { get; set; }
public required int DomainId { get; set; }
public required int UserId { get; set; }
public required string SubdomainName { get; set; }
Expand Down
1 change: 1 addition & 0 deletions Domain/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Domain;

public class User
{
public int Id { get; set; }
public required string Email { get; set; }
public required string Username { get; set; }

Expand Down
30 changes: 30 additions & 0 deletions Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>

</Project>

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

76 changes: 76 additions & 0 deletions Infrastructure/Migrations/20240703191648_initial-migration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Infrastructure.Migrations
{
/// <inheritdoc />
public partial class initialmigration : Migration

Check warning on line 8 in Infrastructure/Migrations/20240703191648_initial-migration.cs

View workflow job for this annotation

GitHub Actions / build

The type name 'initialmigration' only contains lower-cased ascii characters. Such names may become reserved for the language.

Check warning on line 8 in Infrastructure/Migrations/20240703191648_initial-migration.cs

View workflow job for this annotation

GitHub Actions / build

The type name 'initialmigration' only contains lower-cased ascii characters. Such names may become reserved for the language.
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Domains",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Domain = table.Column<string>(type: "nvarchar(max)", nullable: false),
Active = table.Column<bool>(type: "bit", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Domains", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Subdomains",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
DomainId = table.Column<int>(type: "int", nullable: false),
UserId = table.Column<int>(type: "int", nullable: false),
SubdomainName = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Subdomains", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Email = table.Column<string>(type: "nvarchar(450)", nullable: false),
Username = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});

migrationBuilder.CreateIndex(
name: "IX_Users_Email",
table: "Users",
column: "Email",
unique: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Domains");

migrationBuilder.DropTable(
name: "Subdomains");

migrationBuilder.DropTable(
name: "Users");
}
}
}
93 changes: 93 additions & 0 deletions Infrastructure/Migrations/YourLabDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// <auto-generated />
using Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace Infrastructure.Migrations
{
[DbContext(typeof(YourLabDbContext))]
partial class YourLabDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.6")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

modelBuilder.Entity("Domain.Domains", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<bool>("Active")
.HasColumnType("bit");

b.Property<string>("Domain")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("Domains");
});

modelBuilder.Entity("Domain.Subdomains", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<int>("DomainId")
.HasColumnType("int");

b.Property<string>("SubdomainName")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<int>("UserId")
.HasColumnType("int");

b.HasKey("Id");

b.ToTable("Subdomains");
});

modelBuilder.Entity("Domain.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<string>("Email")
.IsRequired()
.HasColumnType("nvarchar(450)");

b.Property<string>("Username")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.HasIndex("Email")
.IsUnique();

b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}
Loading

0 comments on commit 2d0606b

Please sign in to comment.