Setup ef core tools and sqlite database with some entities

This commit is contained in:
2019-04-20 00:39:07 +00:00
parent 4e5142d5ec
commit 0f9a598fae
17 changed files with 424 additions and 9 deletions

View File

@@ -9,4 +9,14 @@
<Folder Include="Repositories\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="mongocsharpdriver" Version="2.8.0" />
<PackageReference Include="RabbitMQ.Client" Version="5.1.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data.Domain
{
public class Course
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Code { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data.Domain
{
public class Phone
{
public Guid Id { get; set; }
public PhoneType Type { get; set; }
public int AreaCode { get; set; }
public int Number { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data.Domain
{
public enum PhoneType
{
HOMEPHONE, CELLPHONE, WORKPHONE
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data.Domain
{
public class Student
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<Phone> Phones { get; set; }
}
}

View File

@@ -0,0 +1,68 @@
// <auto-generated />
using System;
using BrightGlimmer.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace BrightGlimmer.Data.Migrations
{
[DbContext(typeof(SqliteDatabaseContext))]
[Migration("20190420001348_CreateDatabase")]
partial class CreateDatabase
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062");
modelBuilder.Entity("BrightGlimmer.Data.Domain.Phone", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("AreaCode");
b.Property<int>("Number");
b.Property<Guid?>("StudentId");
b.Property<int>("Type");
b.HasKey("Id");
b.HasIndex("StudentId");
b.ToTable("Phone");
});
modelBuilder.Entity("BrightGlimmer.Data.Domain.Student", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Email");
b.Property<string>("FirstName");
b.Property<string>("LastName");
b.Property<string>("MiddleName");
b.HasKey("Id");
b.ToTable("Students");
});
modelBuilder.Entity("BrightGlimmer.Data.Domain.Phone", b =>
{
b.HasOne("BrightGlimmer.Data.Domain.Student")
.WithMany("Phones")
.HasForeignKey("StudentId");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,61 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace BrightGlimmer.Data.Migrations
{
public partial class CreateDatabase : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Students",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
FirstName = table.Column<string>(nullable: true),
MiddleName = table.Column<string>(nullable: true),
LastName = table.Column<string>(nullable: true),
Email = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Students", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Phone",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
Type = table.Column<int>(nullable: false),
AreaCode = table.Column<int>(nullable: false),
Number = table.Column<int>(nullable: false),
StudentId = table.Column<Guid>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Phone", x => x.Id);
table.ForeignKey(
name: "FK_Phone_Students_StudentId",
column: x => x.StudentId,
principalTable: "Students",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Phone_StudentId",
table: "Phone",
column: "StudentId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Phone");
migrationBuilder.DropTable(
name: "Students");
}
}
}

View File

@@ -0,0 +1,66 @@
// <auto-generated />
using System;
using BrightGlimmer.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace BrightGlimmer.Data.Migrations
{
[DbContext(typeof(SqliteDatabaseContext))]
partial class SqliteDatabaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062");
modelBuilder.Entity("BrightGlimmer.Data.Domain.Phone", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("AreaCode");
b.Property<int>("Number");
b.Property<Guid?>("StudentId");
b.Property<int>("Type");
b.HasKey("Id");
b.HasIndex("StudentId");
b.ToTable("Phone");
});
modelBuilder.Entity("BrightGlimmer.Data.Domain.Student", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Email");
b.Property<string>("FirstName");
b.Property<string>("LastName");
b.Property<string>("MiddleName");
b.HasKey("Id");
b.ToTable("Students");
});
modelBuilder.Entity("BrightGlimmer.Data.Domain.Phone", b =>
{
b.HasOne("BrightGlimmer.Data.Domain.Student")
.WithMany("Phones")
.HasForeignKey("StudentId");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,47 @@
using BrightGlimmer.Data.Domain;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BrightGlimmer.Data.Repositories
{
public class StudentRepository
{
private readonly SqliteDatabaseContext context;
public StudentRepository(SqliteDatabaseContext context)
{
this.context = context;
}
public Student Create(Student student)
{
EntityEntry<Student> entry = context.Students.Add(student);
context.SaveChanges();
return entry.Entity;
}
public void Update(Student student)
{
context.SaveChanges();
}
public void Remove(Guid id)
{
context.Students.Remove(GetById(id));
context.SaveChanges();
}
public IQueryable<Student> GetAll()
{
return context.Students;
}
public Student GetById(Guid id)
{
return context.Students.Find(id);
}
}
}

View File

@@ -0,0 +1,24 @@
using BrightGlimmer.Data.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data
{
public class SqliteDatabaseContext : DbContext
{
public SqliteDatabaseContext(DbContextOptions<SqliteDatabaseContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasMany(x => x.Phones);
}
public DbSet<Student> Students { get; set; }
}
}

Binary file not shown.