Skip to content

Commit

Permalink
Create and migrate database using EF Core
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-Schou committed Jul 4, 2022
1 parent 8aea18f commit 55a6530
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
6 changes: 6 additions & 0 deletions MediatR_Demo/MediatR_Demo.csproj
Expand Up @@ -7,6 +7,12 @@
</PropertyGroup>

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

Expand Down
54 changes: 54 additions & 0 deletions MediatR_Demo/Migrations/20220704112336_MovieAdded.Designer.cs

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

34 changes: 34 additions & 0 deletions MediatR_Demo/Migrations/20220704112336_MovieAdded.cs
@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MediatR_Demo.Migrations
{
public partial class MovieAdded : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Movies",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
Genre = table.Column<int>(type: "int", nullable: false),
Rating = table.Column<int>(type: "int", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Movies", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Movies");
}
}
}
52 changes: 52 additions & 0 deletions MediatR_Demo/Migrations/ApplicationDbContextModelSnapshot.cs
@@ -0,0 +1,52 @@
// <auto-generated />
using System;
using MediatR_Demo.Repository.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

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

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);

modelBuilder.Entity("MediatR_Demo.Domain.Entities.Movie.Movie", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"), 1L, 1);
b.Property<string>("Description")
.HasColumnType("nvarchar(max)");
b.Property<int>("Genre")
.HasColumnType("int");
b.Property<int?>("Rating")
.HasColumnType("int");
b.Property<string>("Title")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Movies");
});
#pragma warning restore 612, 618
}
}
}
5 changes: 5 additions & 0 deletions MediatR_Demo/Program.cs
@@ -1,3 +1,6 @@
using MediatR_Demo.Repository.Context;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -6,6 +9,8 @@
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(builder.Configuration.GetConnectionString("Standard")));

var app = builder.Build();

Expand Down
14 changes: 14 additions & 0 deletions MediatR_Demo/Repository/Context/ApplicationDbContext.cs
@@ -0,0 +1,14 @@
using MediatR_Demo.Domain.Entities.Movie;
using Microsoft.EntityFrameworkCore;

namespace MediatR_Demo.Repository.Context
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}

public DbSet<Movie> Movies { get; set; }
}
}
3 changes: 3 additions & 0 deletions MediatR_Demo/appsettings.json
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"Standard": "Server=localhost;Database=mediatr-demo;Trusted_Connection=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down

0 comments on commit 55a6530

Please sign in to comment.