Skip to content

Instantly share code, notes, and snippets.

Avatar
Expecto Patronum!

Nishan Chathuranga Wickramarathna nishanc

Expecto Patronum!
View GitHub Profile
View Models.cs
using System.ComponentModel.DataAnnotations;
public abstract class TPCUser
{
[Key]
public int Id { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string Email { get; set; }
View Migration.cs
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TPTUsers",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Username = table.Column<string>(type: "nvarchar(max)", nullable: true),
Email = table.Column<string>(type: "nvarchar(max)", nullable: true)
View Script.sql
CREATE TABLE [TPTUsers] (
[Id] int NOT NULL IDENTITY,
[Username] nvarchar(max) NULL,
[Email] nvarchar(max) NULL,
CONSTRAINT [PK_TPTUsers] PRIMARY KEY ([Id])
);
GO
CREATE TABLE [TPTStudents] (
[Id] int NOT NULL,
View Models.cs
using System.ComponentModel.DataAnnotations.Schema;
[Table("TPTUsers")]
public class TPTUser
{
public int Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
}
View Models.cs
public class TPHUser
{
public int Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string UserType { get; set; }
}
public class TPHTeacher : TPHUser
{
View Migration.cs
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TPHUsers",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Username = table.Column<string>(type: "nvarchar(max)", nullable: true),
Email = table.Column<string>(type: "nvarchar(max)", nullable: true),
View Table
+----+-----------+--------------------+---------------+-------------+------------+------+-------+
| Id | Username | Email | Discriminator | Designation | Speciality | CGPA | Major |
+----+-----------+--------------------+---------------+-------------+------------+------+-------+
| 1 | TestUser | teacher@sample.com | TPHTeacher | Dr. | Biology | | |
| 2 | TestUser2 | student@sample.com | TPHStudent | | | 3.5 | IT |
+----+-----------+--------------------+---------------+-------------+------------+------+-------+
View Migration.cs
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TPHUsers",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Username = table.Column<string>(type: "nvarchar(max)", nullable: true),
Email = table.Column<string>(type: "nvarchar(max)", nullable: true),
View Models.cs
public class TPHUser
{
public int Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
}
public class TPHTeacher : TPHUser
{
public string Designation { get; set; }
View SampleContext.cs
public class SampleContext : DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Category> Categories { get; set; }
}