From 1eb871960c9856cd5fc302dbb8f6ea52d53a31d3 Mon Sep 17 00:00:00 2001 From: Sachith Samarakoon Date: Fri, 16 Sep 2022 10:39:41 +0530 Subject: [PATCH 1/8] add database models and initial migration --- .../TodoApp.DataAccess/DataModels/Todo.cs | 21 +++++++ .../DataModels/TodoContext.cs | 23 +++++++ ...0220916050709_InitialMigration.Designer.cs | 62 +++++++++++++++++++ .../20220916050709_InitialMigration.cs | 37 +++++++++++ .../Migrations/TodoContextModelSnapshot.cs | 60 ++++++++++++++++++ .../TodoApp.DataAccess.csproj | 22 +++++++ .../Source/TodoApp/TodoApp.Portal/Program.cs | 11 ++++ .../TodoApp.Portal/TodoApp.Portal.csproj | 11 ++++ .../appsettings.Development.json | 3 + .../Source/TodoApp/TodoApp.sln | 8 ++- 10 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.Designer.cs create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.cs create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/TodoApp.DataAccess.csproj diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs new file mode 100644 index 0000000..9d995d3 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs @@ -0,0 +1,21 @@ + +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace TodoApp.DataAccess.DataModels +{ + [Table("Todo")] + public class Todo + { + public long Id { get; set; } + [Required] + public string Name { get; set; } + public string? Description { get; set; } + public bool IsCompleted { get; set; } + [Required] + public DateTime CreatedTime { get; set; } + public DateTime? CompletedTime { get; set; } + [Required] + public string CreatedUser { get; set; } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs new file mode 100644 index 0000000..3bce33d --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; + +namespace TodoApp.DataAccess.DataModels +{ + public class TodoContext : DbContext + { + private string _connection_string; + public TodoContext(IConfiguration configuration) + { + _connection_string = configuration.GetConnectionString("connection_string"); + } + + public DbSet Todos { get; set; } + + public string Name => nameof(TodoContext); + public string Provider => nameof(Microsoft.EntityFrameworkCore); + + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder.UseSqlServer(_connection_string); + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.Designer.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.Designer.cs new file mode 100644 index 0000000..c523be6 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.Designer.cs @@ -0,0 +1,62 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TodoApp.DataAccess.DataModels; + +#nullable disable + +namespace TodoApp.DataAccess.Migrations +{ + [DbContext(typeof(TodoContext))] + [Migration("20220916050709_InitialMigration")] + partial class InitialMigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("CompletedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedUser") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("IsCompleted") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Todo"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.cs new file mode 100644 index 0000000..3369fdf --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.cs @@ -0,0 +1,37 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TodoApp.DataAccess.Migrations +{ + public partial class InitialMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Todo", + columns: table => new + { + Id = table.Column(type: "bigint", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: true), + IsCompleted = table.Column(type: "bit", nullable: false), + CreatedTime = table.Column(type: "datetime2", nullable: false), + CompletedTime = table.Column(type: "datetime2", nullable: true), + CreatedUser = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Todo", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Todo"); + } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs new file mode 100644 index 0000000..9df6878 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs @@ -0,0 +1,60 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TodoApp.DataAccess.DataModels; + +#nullable disable + +namespace TodoApp.DataAccess.Migrations +{ + [DbContext(typeof(TodoContext))] + partial class TodoContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("CompletedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedUser") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("IsCompleted") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Todo"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/TodoApp.DataAccess.csproj b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/TodoApp.DataAccess.csproj new file mode 100644 index 0000000..d41746b --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/TodoApp.DataAccess.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs index 0727468..c36e3b1 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs @@ -1,10 +1,21 @@ +using Microsoft.EntityFrameworkCore; +using TodoApp.DataAccess.DataModels; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); +builder.Services.AddDbContext(); var app = builder.Build(); +// Migrate database +using (var scope = app.Services.CreateScope()) +{ + var dataContext = scope.ServiceProvider.GetRequiredService(); + dataContext.Database.Migrate(); +} + // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj index c78c9c7..f629d04 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj @@ -6,4 +6,15 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/appsettings.Development.json b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/appsettings.Development.json index 0c208ae..d4b7b43 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/appsettings.Development.json +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "ConnectionStrings": { + "connection_string": "Server=.;Database=TodoApp;Trusted_Connection=True;MultipleActiveResultSets=true" } } diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.sln b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.sln index 0151a5c..b7aebab 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.sln +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.1.32210.238 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoApp.Portal", "TodoApp.Portal\TodoApp.Portal.csproj", "{46DCD6D6-EA0A-4946-9E15-781754A38D80}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TodoApp.Portal", "TodoApp.Portal\TodoApp.Portal.csproj", "{46DCD6D6-EA0A-4946-9E15-781754A38D80}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoApp.DataAccess", "TodoApp.DataAccess\TodoApp.DataAccess.csproj", "{D5C0D56E-DB0E-486B-A6C6-9DFDA7F91578}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {46DCD6D6-EA0A-4946-9E15-781754A38D80}.Debug|Any CPU.Build.0 = Debug|Any CPU {46DCD6D6-EA0A-4946-9E15-781754A38D80}.Release|Any CPU.ActiveCfg = Release|Any CPU {46DCD6D6-EA0A-4946-9E15-781754A38D80}.Release|Any CPU.Build.0 = Release|Any CPU + {D5C0D56E-DB0E-486B-A6C6-9DFDA7F91578}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D5C0D56E-DB0E-486B-A6C6-9DFDA7F91578}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D5C0D56E-DB0E-486B-A6C6-9DFDA7F91578}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D5C0D56E-DB0E-486B-A6C6-9DFDA7F91578}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 8cde193d51c62ffa1a00ccc905fa65e84ed860dc Mon Sep 17 00:00:00 2001 From: Ganeshmoorthi Dilshan Prasad Date: Mon, 19 Sep 2022 20:51:47 +0530 Subject: [PATCH 2/8] Added User class and made some minor changes --- .../TodoApp.DataAccess/DataModels/Todo.cs | 2 +- .../DataModels/TodoContext.cs | 1 + .../TodoApp.DataAccess/DataModels/User.cs | 20 ++++++++++++ ...220919141048_InitialMigration.Designer.cs} | 32 ++++++++++++++++--- ....cs => 20220919141048_InitialMigration.cs} | 20 +++++++++++- .../Migrations/TodoContextModelSnapshot.cs | 30 +++++++++++++++-- 6 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/User.cs rename azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/{20220916050709_InitialMigration.Designer.cs => 20220919141048_InitialMigration.Designer.cs} (67%) rename azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/{20220916050709_InitialMigration.cs => 20220919141048_InitialMigration.cs} (60%) diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs index 9d995d3..d3579a4 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs @@ -16,6 +16,6 @@ public class Todo public DateTime CreatedTime { get; set; } public DateTime? CompletedTime { get; set; } [Required] - public string CreatedUser { get; set; } + public long CreatedUser { get; set; } } } diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs index 3bce33d..c45d50f 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs @@ -11,6 +11,7 @@ public TodoContext(IConfiguration configuration) _connection_string = configuration.GetConnectionString("connection_string"); } + public DbSet Users { get; set; } public DbSet Todos { get; set; } public string Name => nameof(TodoContext); diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/User.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/User.cs new file mode 100644 index 0000000..3890e54 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/User.cs @@ -0,0 +1,20 @@ + +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace TodoApp.DataAccess.DataModels +{ + [Table("User")] + public class User + { + public long Id { get; set; } + [Required] + public string Email { get; set; } + [Required] + public string FirstName { get; set; } + [Required] + public string LastName { get; set; } + + + } +} \ No newline at end of file diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.Designer.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.Designer.cs similarity index 67% rename from azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.Designer.cs rename to azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.Designer.cs index c523be6..bb92d53 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.Designer.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.Designer.cs @@ -12,7 +12,7 @@ namespace TodoApp.DataAccess.Migrations { [DbContext(typeof(TodoContext))] - [Migration("20220916050709_InitialMigration")] + [Migration("20220919141048_InitialMigration")] partial class InitialMigration { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -38,9 +38,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("CreatedTime") .HasColumnType("datetime2"); - b.Property("CreatedUser") - .IsRequired() - .HasColumnType("nvarchar(max)"); + b.Property("CreatedUser") + .HasColumnType("bigint"); b.Property("Description") .HasColumnType("nvarchar(max)"); @@ -56,6 +55,31 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.ToTable("Todo"); }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("User"); + }); #pragma warning restore 612, 618 } } diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.cs similarity index 60% rename from azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.cs rename to azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.cs index 3369fdf..67a2913 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220916050709_InitialMigration.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.cs @@ -20,18 +20,36 @@ protected override void Up(MigrationBuilder migrationBuilder) IsCompleted = table.Column(type: "bit", nullable: false), CreatedTime = table.Column(type: "datetime2", nullable: false), CompletedTime = table.Column(type: "datetime2", nullable: true), - CreatedUser = table.Column(type: "nvarchar(max)", nullable: false) + CreatedUser = table.Column(type: "bigint", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Todo", x => x.Id); }); + + migrationBuilder.CreateTable( + name: "User", + columns: table => new + { + Id = table.Column(type: "bigint", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Email = table.Column(type: "nvarchar(max)", nullable: false), + FirstName = table.Column(type: "nvarchar(max)", nullable: false), + LastName = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_User", x => x.Id); + }); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "Todo"); + + migrationBuilder.DropTable( + name: "User"); } } } diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs index 9df6878..72aac2c 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs @@ -36,9 +36,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("CreatedTime") .HasColumnType("datetime2"); - b.Property("CreatedUser") - .IsRequired() - .HasColumnType("nvarchar(max)"); + b.Property("CreatedUser") + .HasColumnType("bigint"); b.Property("Description") .HasColumnType("nvarchar(max)"); @@ -54,6 +53,31 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("Todo"); }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("User"); + }); #pragma warning restore 612, 618 } } From ff4f1a6fdd87a3aa98055dedfcb77d13bce436be Mon Sep 17 00:00:00 2001 From: Ganeshmoorthi Dilshan Prasad Date: Wed, 21 Sep 2022 22:53:52 +0530 Subject: [PATCH 3/8] moved UseSqlServer function to Program.cs --- .../TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs | 9 +-------- ...er.cs => 20220920185603_InitialMigration.Designer.cs} | 2 +- ...alMigration.cs => 20220920185603_InitialMigration.cs} | 0 .../Source/TodoApp/TodoApp.Portal/Program.cs | 7 +++++-- 4 files changed, 7 insertions(+), 11 deletions(-) rename azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/{20220919141048_InitialMigration.Designer.cs => 20220920185603_InitialMigration.Designer.cs} (98%) rename azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/{20220919141048_InitialMigration.cs => 20220920185603_InitialMigration.cs} (100%) diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs index c45d50f..c171e01 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs @@ -5,11 +5,7 @@ namespace TodoApp.DataAccess.DataModels { public class TodoContext : DbContext { - private string _connection_string; - public TodoContext(IConfiguration configuration) - { - _connection_string = configuration.GetConnectionString("connection_string"); - } + public TodoContext(DbContextOptions options) : base(options) { } public DbSet Users { get; set; } public DbSet Todos { get; set; } @@ -17,8 +13,5 @@ public TodoContext(IConfiguration configuration) public string Name => nameof(TodoContext); public string Provider => nameof(Microsoft.EntityFrameworkCore); - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - => optionsBuilder.UseSqlServer(_connection_string); } } diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.Designer.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.Designer.cs similarity index 98% rename from azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.Designer.cs rename to azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.Designer.cs index bb92d53..9052069 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.Designer.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.Designer.cs @@ -12,7 +12,7 @@ namespace TodoApp.DataAccess.Migrations { [DbContext(typeof(TodoContext))] - [Migration("20220919141048_InitialMigration")] + [Migration("20220920185603_InitialMigration")] partial class InitialMigration { protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.cs similarity index 100% rename from azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220919141048_InitialMigration.cs rename to azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.cs diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs index c36e3b1..252decc 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs @@ -5,7 +5,10 @@ // Add services to the container. builder.Services.AddControllersWithViews(); -builder.Services.AddDbContext(); + +// Add DbContext +var connectionString = builder.Configuration.GetConnectionString("connection_string"); +builder.Services.AddDbContext(options => options.UseSqlServer(connectionString)); var app = builder.Build(); @@ -35,4 +38,4 @@ name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); -app.Run(); +app.Run(); \ No newline at end of file From e87b50e18a40d14365cbbf686df2eaa899876404 Mon Sep 17 00:00:00 2001 From: Indunil Withana Date: Sat, 15 Oct 2022 19:04:23 +0530 Subject: [PATCH 4/8] AFG: Added initial UI components --- .../Controllers/TodoController.cs | 160 ++++++++++++++++++ .../Source/TodoApp/TodoApp.Portal/Program.cs | 2 +- .../TodoApp.Portal/TodoApp.Portal.csproj | 8 +- .../TodoApp.Portal/Views/Todo/Create.cshtml | 59 +++++++ .../TodoApp.Portal/Views/Todo/Delete.cshtml | 58 +++++++ .../TodoApp.Portal/Views/Todo/Details.cshtml | 55 ++++++ .../TodoApp.Portal/Views/Todo/Edit.cshtml | 60 +++++++ .../TodoApp.Portal/Views/Todo/Index.cshtml | 66 ++++++++ 8 files changed, 466 insertions(+), 2 deletions(-) create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Create.cshtml create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Delete.cshtml create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Details.cshtml create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Index.cshtml diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs new file mode 100644 index 0000000..8393a83 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs @@ -0,0 +1,160 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.EntityFrameworkCore; +using TodoApp.DataAccess.DataModels; + +namespace TodoApp.Portal.Controllers +{ + public class TodoController : Controller + { + private readonly TodoContext _context; + + public TodoController(TodoContext context) + { + _context = context; + } + + // GET: Todo + public async Task Index() + { + return View(await _context.Todos.ToListAsync()); + } + + // GET: Todo/Details/5 + public async Task Details(long? id) + { + if (id == null || _context.Todos == null) + { + return NotFound(); + } + + var todo = await _context.Todos + .FirstOrDefaultAsync(m => m.Id == id); + if (todo == null) + { + return NotFound(); + } + + return View(todo); + } + + // GET: Todo/Create + public IActionResult Create() + { + return View(); + } + + // POST: Todo/Create + // To protect from overposting attacks, enable the specific properties you want to bind to. + // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Create([Bind("Id,Name,Description,IsCompleted,CreatedTime,CompletedTime,CreatedUser")] Todo todo) + { + if (ModelState.IsValid) + { + _context.Add(todo); + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + return View(todo); + } + + // GET: Todo/Edit/5 + public async Task Edit(long? id) + { + if (id == null || _context.Todos == null) + { + return NotFound(); + } + + var todo = await _context.Todos.FindAsync(id); + if (todo == null) + { + return NotFound(); + } + return View(todo); + } + + // POST: Todo/Edit/5 + // To protect from overposting attacks, enable the specific properties you want to bind to. + // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Edit(long id, [Bind("Id,Name,Description,IsCompleted,CreatedTime,CompletedTime,CreatedUser")] Todo todo) + { + if (id != todo.Id) + { + return NotFound(); + } + + if (ModelState.IsValid) + { + try + { + _context.Update(todo); + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!TodoExists(todo.Id)) + { + return NotFound(); + } + else + { + throw; + } + } + return RedirectToAction(nameof(Index)); + } + return View(todo); + } + + // GET: Todo/Delete/5 + public async Task Delete(long? id) + { + if (id == null || _context.Todos == null) + { + return NotFound(); + } + + var todo = await _context.Todos + .FirstOrDefaultAsync(m => m.Id == id); + if (todo == null) + { + return NotFound(); + } + + return View(todo); + } + + // POST: Todo/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public async Task DeleteConfirmed(long id) + { + if (_context.Todos == null) + { + return Problem("Entity set 'TodoContext.Todos' is null."); + } + var todo = await _context.Todos.FindAsync(id); + if (todo != null) + { + _context.Todos.Remove(todo); + } + + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + + private bool TodoExists(long id) + { + return _context.Todos.Any(e => e.Id == id); + } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs index 252decc..d5059ec 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs @@ -36,6 +36,6 @@ app.MapControllerRoute( name: "default", - pattern: "{controller=Home}/{action=Index}/{id?}"); + pattern: "{controller=Todo}/{action=Index}/{id?}"); app.Run(); \ No newline at end of file diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj index f629d04..647474e 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj @@ -7,10 +7,16 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Create.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Create.cshtml new file mode 100644 index 0000000..b866c2b --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Create.cshtml @@ -0,0 +1,59 @@ +@model TodoApp.DataAccess.DataModels.Todo + +@{ + ViewData["Title"] = "Create"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + +

Create

+ +

Todo

+
+
+
+
+
+
+ + + +
+
+ + + +
+
+ +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ +
+
+
+
+ + + +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Delete.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Delete.cshtml new file mode 100644 index 0000000..17ee957 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Delete.cshtml @@ -0,0 +1,58 @@ +@model TodoApp.DataAccess.DataModels.Todo + +@{ + ViewData["Title"] = "Delete"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + +

Delete

+ +

Are you sure you want to delete this?

+
+

Todo

+
+
+
+ @Html.DisplayNameFor(model => model.Name) +
+
+ @Html.DisplayFor(model => model.Name) +
+
+ @Html.DisplayNameFor(model => model.Description) +
+
+ @Html.DisplayFor(model => model.Description) +
+
+ @Html.DisplayNameFor(model => model.IsCompleted) +
+
+ @Html.DisplayFor(model => model.IsCompleted) +
+
+ @Html.DisplayNameFor(model => model.CreatedTime) +
+
+ @Html.DisplayFor(model => model.CreatedTime) +
+
+ @Html.DisplayNameFor(model => model.CompletedTime) +
+
+ @Html.DisplayFor(model => model.CompletedTime) +
+
+ @Html.DisplayNameFor(model => model.CreatedUser) +
+
+ @Html.DisplayFor(model => model.CreatedUser) +
+
+ +
+ + | + Back to List +
+
diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Details.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Details.cshtml new file mode 100644 index 0000000..2a56263 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Details.cshtml @@ -0,0 +1,55 @@ +@model TodoApp.DataAccess.DataModels.Todo + +@{ + ViewData["Title"] = "Details"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + +

Details

+ +
+

Todo

+
+
+
+ @Html.DisplayNameFor(model => model.Name) +
+
+ @Html.DisplayFor(model => model.Name) +
+
+ @Html.DisplayNameFor(model => model.Description) +
+
+ @Html.DisplayFor(model => model.Description) +
+
+ @Html.DisplayNameFor(model => model.IsCompleted) +
+
+ @Html.DisplayFor(model => model.IsCompleted) +
+
+ @Html.DisplayNameFor(model => model.CreatedTime) +
+
+ @Html.DisplayFor(model => model.CreatedTime) +
+
+ @Html.DisplayNameFor(model => model.CompletedTime) +
+
+ @Html.DisplayFor(model => model.CompletedTime) +
+
+ @Html.DisplayNameFor(model => model.CreatedUser) +
+
+ @Html.DisplayFor(model => model.CreatedUser) +
+
+
+ diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml new file mode 100644 index 0000000..8a9501a --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml @@ -0,0 +1,60 @@ +@model TodoApp.DataAccess.DataModels.Todo + +@{ + ViewData["Title"] = "Edit"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + +

Edit

+ +

Todo

+
+
+
+
+
+ +
+ + + +
+
+ + + +
+
+ +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ +
+
+
+
+ + + +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Index.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Index.cshtml new file mode 100644 index 0000000..3e989fd --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Index.cshtml @@ -0,0 +1,66 @@ +@model IEnumerable + +@{ + ViewData["Title"] = "Index"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + +

Index

+ +

+ Create New +

+ + + + + + + + + + + + + +@foreach (var item in Model) { + + + + + + + + + +} + +
+ @Html.DisplayNameFor(model => model.Name) + + @Html.DisplayNameFor(model => model.Description) + + @Html.DisplayNameFor(model => model.IsCompleted) + + @Html.DisplayNameFor(model => model.CreatedTime) + + @Html.DisplayNameFor(model => model.CompletedTime) + + @Html.DisplayNameFor(model => model.CreatedUser) +
+ @Html.DisplayFor(modelItem => item.Name) + + @Html.DisplayFor(modelItem => item.Description) + + @Html.DisplayFor(modelItem => item.IsCompleted) + + @Html.DisplayFor(modelItem => item.CreatedTime) + + @Html.DisplayFor(modelItem => item.CompletedTime) + + @Html.DisplayFor(modelItem => item.CreatedUser) + + Edit | + Details | + Delete +
From 4866d7ddb45bf2324adbdf1ff1e4dc99bb3c37cb Mon Sep 17 00:00:00 2001 From: Indunil Withana Date: Sun, 16 Oct 2022 21:36:01 +0530 Subject: [PATCH 5/8] AFG: Added foreign key for the user --- .../TodoApp.DataAccess/DataModels/Todo.cs | 9 ++ .../TodoApp.DataAccess/DataModels/User.cs | 5 + .../20221016154222_addForeignKeys.Designer.cs | 104 +++++++++++++++ .../20221016154222_addForeignKeys.cs | 36 ++++++ .../Migrations/TodoContextModelSnapshot.cs | 20 ++- .../Controllers/TodoController.cs | 9 +- .../Views/Shared/_Layout.cshtml | 23 +--- .../TodoApp.Portal/Views/Todo/Create.cshtml | 10 -- .../TodoApp.Portal/Views/Todo/Edit.cshtml | 10 -- .../TodoApp.Portal/Views/Todo/Index.cshtml | 120 +++++++++--------- 10 files changed, 242 insertions(+), 104 deletions(-) create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.Designer.cs create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.cs diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs index d3579a4..360ab0e 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/Todo.cs @@ -8,14 +8,23 @@ namespace TodoApp.DataAccess.DataModels public class Todo { public long Id { get; set; } + [Required] public string Name { get; set; } + public string? Description { get; set; } + public bool IsCompleted { get; set; } + [Required] public DateTime CreatedTime { get; set; } + public DateTime? CompletedTime { get; set; } + [Required] + [ForeignKey("User")] public long CreatedUser { get; set; } + + public virtual User? User { get; set; } } } diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/User.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/User.cs index 3890e54..8649f94 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/User.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/User.cs @@ -8,13 +8,18 @@ namespace TodoApp.DataAccess.DataModels public class User { public long Id { get; set; } + [Required] public string Email { get; set; } + [Required] public string FirstName { get; set; } + [Required] public string LastName { get; set; } + public ICollection ToDos { get; set; } + } } \ No newline at end of file diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.Designer.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.Designer.cs new file mode 100644 index 0000000..39a30a0 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.Designer.cs @@ -0,0 +1,104 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TodoApp.DataAccess.DataModels; + +#nullable disable + +namespace TodoApp.DataAccess.Migrations +{ + [DbContext(typeof(TodoContext))] + [Migration("20221016154222_addForeignKeys")] + partial class addForeignKeys + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("CompletedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedUser") + .HasColumnType("bigint"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("IsCompleted") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("CreatedUser"); + + b.ToTable("Todo"); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("User"); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => + { + b.HasOne("TodoApp.DataAccess.DataModels.User", "User") + .WithMany("ToDos") + .HasForeignKey("CreatedUser") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => + { + b.Navigation("ToDos"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.cs new file mode 100644 index 0000000..61bb682 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TodoApp.DataAccess.Migrations +{ + public partial class addForeignKeys : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateIndex( + name: "IX_Todo_CreatedUser", + table: "Todo", + column: "CreatedUser"); + + migrationBuilder.AddForeignKey( + name: "FK_Todo_User_CreatedUser", + table: "Todo", + column: "CreatedUser", + principalTable: "User", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Todo_User_CreatedUser", + table: "Todo"); + + migrationBuilder.DropIndex( + name: "IX_Todo_CreatedUser", + table: "Todo"); + } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs index 72aac2c..ff6920f 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs @@ -17,7 +17,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("ProductVersion", "6.0.10") .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); @@ -51,6 +51,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); + b.HasIndex("CreatedUser"); + b.ToTable("Todo"); }); @@ -78,6 +80,22 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("User"); }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => + { + b.HasOne("TodoApp.DataAccess.DataModels.User", "User") + .WithMany("ToDos") + .HasForeignKey("CreatedUser") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => + { + b.Navigation("ToDos"); + }); #pragma warning restore 612, 618 } } diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs index 8393a83..9d5435e 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs @@ -53,10 +53,13 @@ public IActionResult Create() // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public async Task Create([Bind("Id,Name,Description,IsCompleted,CreatedTime,CompletedTime,CreatedUser")] Todo todo) + public async Task Create([Bind("Id,Name,Description,IsCompleted,CompletedTime")] Todo todo) { + ModelState.Remove("User"); if (ModelState.IsValid) { + todo.CreatedUser = 3; + todo.CreatedTime = DateTime.Now; _context.Add(todo); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); @@ -85,13 +88,15 @@ public async Task Edit(long? id) // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public async Task Edit(long id, [Bind("Id,Name,Description,IsCompleted,CreatedTime,CompletedTime,CreatedUser")] Todo todo) + public async Task Edit(long id, [Bind("Id,Name,Description,IsCompleted,CompletedTime")] Todo todo) { if (id != todo.Id) { return NotFound(); } + todo.CreatedUser = 3; + if (ModelState.IsValid) { try diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Shared/_Layout.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Shared/_Layout.cshtml index 7081751..f842028 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Shared/_Layout.cshtml +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Shared/_Layout.cshtml @@ -6,27 +6,13 @@ @ViewData["Title"] - TodoApp.Portal - +
@@ -36,11 +22,6 @@ -
-
- © 2022 - TodoApp.Portal - Privacy -
-
diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Create.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Create.cshtml index b866c2b..7ed5258 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Create.cshtml +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Create.cshtml @@ -28,21 +28,11 @@ @Html.DisplayNameFor(model => model.IsCompleted) -
- - - -
-
- - - -
diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml index 8a9501a..9931ba2 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml @@ -29,21 +29,11 @@ @Html.DisplayNameFor(model => model.IsCompleted) -
- - - -
-
- - - -
diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Index.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Index.cshtml index 3e989fd..07efcac 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Index.cshtml +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Index.cshtml @@ -1,66 +1,66 @@ @model IEnumerable @{ - ViewData["Title"] = "Index"; + ViewData["Title"] = "Task To Do"; Layout = "~/Views/Shared/_Layout.cshtml"; } -

Index

- -

- Create New -

- - - - - - - - - - - - - -@foreach (var item in Model) { - - - - - - - - - -} - -
- @Html.DisplayNameFor(model => model.Name) - - @Html.DisplayNameFor(model => model.Description) - - @Html.DisplayNameFor(model => model.IsCompleted) - - @Html.DisplayNameFor(model => model.CreatedTime) - - @Html.DisplayNameFor(model => model.CompletedTime) - - @Html.DisplayNameFor(model => model.CreatedUser) -
- @Html.DisplayFor(modelItem => item.Name) - - @Html.DisplayFor(modelItem => item.Description) - - @Html.DisplayFor(modelItem => item.IsCompleted) - - @Html.DisplayFor(modelItem => item.CreatedTime) - - @Html.DisplayFor(modelItem => item.CompletedTime) - - @Html.DisplayFor(modelItem => item.CreatedUser) - - Edit | - Details | - Delete -
+
+

+ Create New +

+ + + + + + + + + + + + + + @foreach (var item in Model) { + + + + + + + + + + } + +
+ @Html.DisplayNameFor(model => model.Name) + + @Html.DisplayNameFor(model => model.Description) + + @Html.DisplayNameFor(model => model.IsCompleted) + + @Html.DisplayNameFor(model => model.CreatedTime) + + @Html.DisplayNameFor(model => model.CompletedTime) + + @Html.DisplayNameFor(model => model.CreatedUser) +
+ @Html.DisplayFor(modelItem => item.Name) + + @Html.DisplayFor(modelItem => item.Description) + + @Html.DisplayFor(modelItem => item.IsCompleted) + + @Html.DisplayFor(modelItem => item.CreatedTime) + + @Html.DisplayFor(modelItem => item.CompletedTime) + + @Html.DisplayFor(modelItem => item.User.Email) + + Edit | + Details | + Delete +
+
From b181ba4cf3962c0fb7ae3cb55af1511197511c9e Mon Sep 17 00:00:00 2001 From: Indunil Withana Date: Fri, 21 Oct 2022 11:35:51 +0530 Subject: [PATCH 6/8] AFG: Added user migration --- .../DataModels/TodoContext.cs | 8 ++++++ .../20221021053750_addUserInitialization.cs | 25 +++++++++++++++++++ .../Controllers/TodoController.cs | 6 ++--- .../TodoApp.Portal/Views/Todo/Edit.cshtml | 5 ++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021053750_addUserInitialization.cs diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs index c171e01..41d6f7b 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/DataModels/TodoContext.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using System.Reflection.Metadata; namespace TodoApp.DataAccess.DataModels { @@ -13,5 +14,12 @@ public TodoContext(DbContextOptions options) : base(options) { } public string Name => nameof(TodoContext); public string Provider => nameof(Microsoft.EntityFrameworkCore); + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .HasData( + new User { Id = 1, FirstName = "Admin", LastName = "Admin", Email = "indunilw@99x.io" }); + } + } } diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021053750_addUserInitialization.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021053750_addUserInitialization.cs new file mode 100644 index 0000000..ebec041 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021053750_addUserInitialization.cs @@ -0,0 +1,25 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TodoApp.DataAccess.Migrations +{ + public partial class addUserInitialization : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.InsertData( + table: "User", + columns: new[] { "Id", "Email", "FirstName", "LastName" }, + values: new object[] { 1L, "indunilw@99x.io", "Admin", "Admin" }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DeleteData( + table: "User", + keyColumn: "Id", + keyValue: 1L); + } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs index 9d5435e..3c3219e 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/TodoController.cs @@ -58,7 +58,7 @@ public async Task Create([Bind("Id,Name,Description,IsCompleted,C ModelState.Remove("User"); if (ModelState.IsValid) { - todo.CreatedUser = 3; + todo.CreatedUser = todo.CreatedUser == 0 ? 1 : todo.CreatedUser; todo.CreatedTime = DateTime.Now; _context.Add(todo); await _context.SaveChangesAsync(); @@ -88,15 +88,13 @@ public async Task Edit(long? id) // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public async Task Edit(long id, [Bind("Id,Name,Description,IsCompleted,CompletedTime")] Todo todo) + public async Task Edit(long id, [Bind("Id,Name,Description,IsCompleted,CompletedTime,CreatedUser")] Todo todo) { if (id != todo.Id) { return NotFound(); } - todo.CreatedUser = 3; - if (ModelState.IsValid) { try diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml index 9931ba2..9152d67 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Todo/Edit.cshtml @@ -34,6 +34,11 @@ +
+ + + +
From 7a3093e7531a18c710de436ab05a9363abf320ec Mon Sep 17 00:00:00 2001 From: Indunil Withana Date: Fri, 21 Oct 2022 11:53:22 +0530 Subject: [PATCH 7/8] AFG: Removed migrations --- ...0220920185603_InitialMigration.Designer.cs | 86 --------------- .../20220920185603_InitialMigration.cs | 55 --------- .../20221016154222_addForeignKeys.Designer.cs | 104 ------------------ .../20221016154222_addForeignKeys.cs | 36 ------ .../20221021053750_addUserInitialization.cs | 25 ----- .../Migrations/TodoContextModelSnapshot.cs | 102 ----------------- 6 files changed, 408 deletions(-) delete mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.Designer.cs delete mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.cs delete mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.Designer.cs delete mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.cs delete mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021053750_addUserInitialization.cs delete mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.Designer.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.Designer.cs deleted file mode 100644 index 9052069..0000000 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.Designer.cs +++ /dev/null @@ -1,86 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using TodoApp.DataAccess.DataModels; - -#nullable disable - -namespace TodoApp.DataAccess.Migrations -{ - [DbContext(typeof(TodoContext))] - [Migration("20220920185603_InitialMigration")] - partial class InitialMigration - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "6.0.9") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("bigint"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); - - b.Property("CompletedTime") - .HasColumnType("datetime2"); - - b.Property("CreatedTime") - .HasColumnType("datetime2"); - - b.Property("CreatedUser") - .HasColumnType("bigint"); - - b.Property("Description") - .HasColumnType("nvarchar(max)"); - - b.Property("IsCompleted") - .HasColumnType("bit"); - - b.Property("Name") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.ToTable("Todo"); - }); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("bigint"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); - - b.Property("Email") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("FirstName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("LastName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.ToTable("User"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.cs deleted file mode 100644 index 67a2913..0000000 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20220920185603_InitialMigration.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace TodoApp.DataAccess.Migrations -{ - public partial class InitialMigration : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Todo", - columns: table => new - { - Id = table.Column(type: "bigint", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - Name = table.Column(type: "nvarchar(max)", nullable: false), - Description = table.Column(type: "nvarchar(max)", nullable: true), - IsCompleted = table.Column(type: "bit", nullable: false), - CreatedTime = table.Column(type: "datetime2", nullable: false), - CompletedTime = table.Column(type: "datetime2", nullable: true), - CreatedUser = table.Column(type: "bigint", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Todo", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "User", - columns: table => new - { - Id = table.Column(type: "bigint", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - Email = table.Column(type: "nvarchar(max)", nullable: false), - FirstName = table.Column(type: "nvarchar(max)", nullable: false), - LastName = table.Column(type: "nvarchar(max)", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_User", x => x.Id); - }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Todo"); - - migrationBuilder.DropTable( - name: "User"); - } - } -} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.Designer.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.Designer.cs deleted file mode 100644 index 39a30a0..0000000 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.Designer.cs +++ /dev/null @@ -1,104 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using TodoApp.DataAccess.DataModels; - -#nullable disable - -namespace TodoApp.DataAccess.Migrations -{ - [DbContext(typeof(TodoContext))] - [Migration("20221016154222_addForeignKeys")] - partial class addForeignKeys - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "6.0.10") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("bigint"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); - - b.Property("CompletedTime") - .HasColumnType("datetime2"); - - b.Property("CreatedTime") - .HasColumnType("datetime2"); - - b.Property("CreatedUser") - .HasColumnType("bigint"); - - b.Property("Description") - .HasColumnType("nvarchar(max)"); - - b.Property("IsCompleted") - .HasColumnType("bit"); - - b.Property("Name") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.HasIndex("CreatedUser"); - - b.ToTable("Todo"); - }); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("bigint"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); - - b.Property("Email") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("FirstName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("LastName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.ToTable("User"); - }); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => - { - b.HasOne("TodoApp.DataAccess.DataModels.User", "User") - .WithMany("ToDos") - .HasForeignKey("CreatedUser") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("User"); - }); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => - { - b.Navigation("ToDos"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.cs deleted file mode 100644 index 61bb682..0000000 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221016154222_addForeignKeys.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace TodoApp.DataAccess.Migrations -{ - public partial class addForeignKeys : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateIndex( - name: "IX_Todo_CreatedUser", - table: "Todo", - column: "CreatedUser"); - - migrationBuilder.AddForeignKey( - name: "FK_Todo_User_CreatedUser", - table: "Todo", - column: "CreatedUser", - principalTable: "User", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropForeignKey( - name: "FK_Todo_User_CreatedUser", - table: "Todo"); - - migrationBuilder.DropIndex( - name: "IX_Todo_CreatedUser", - table: "Todo"); - } - } -} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021053750_addUserInitialization.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021053750_addUserInitialization.cs deleted file mode 100644 index ebec041..0000000 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021053750_addUserInitialization.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace TodoApp.DataAccess.Migrations -{ - public partial class addUserInitialization : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.InsertData( - table: "User", - columns: new[] { "Id", "Email", "FirstName", "LastName" }, - values: new object[] { 1L, "indunilw@99x.io", "Admin", "Admin" }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DeleteData( - table: "User", - keyColumn: "Id", - keyValue: 1L); - } - } -} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs deleted file mode 100644 index ff6920f..0000000 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs +++ /dev/null @@ -1,102 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using TodoApp.DataAccess.DataModels; - -#nullable disable - -namespace TodoApp.DataAccess.Migrations -{ - [DbContext(typeof(TodoContext))] - partial class TodoContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "6.0.10") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("bigint"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); - - b.Property("CompletedTime") - .HasColumnType("datetime2"); - - b.Property("CreatedTime") - .HasColumnType("datetime2"); - - b.Property("CreatedUser") - .HasColumnType("bigint"); - - b.Property("Description") - .HasColumnType("nvarchar(max)"); - - b.Property("IsCompleted") - .HasColumnType("bit"); - - b.Property("Name") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.HasIndex("CreatedUser"); - - b.ToTable("Todo"); - }); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("bigint"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); - - b.Property("Email") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("FirstName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("LastName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.ToTable("User"); - }); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => - { - b.HasOne("TodoApp.DataAccess.DataModels.User", "User") - .WithMany("ToDos") - .HasForeignKey("CreatedUser") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("User"); - }); - - modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => - { - b.Navigation("ToDos"); - }); -#pragma warning restore 612, 618 - } - } -} From db970e3153eb9288bf340bbc86c9f22f8d4236b6 Mon Sep 17 00:00:00 2001 From: Indunil Withana Date: Fri, 21 Oct 2022 11:56:22 +0530 Subject: [PATCH 8/8] AFG: Added all migrations --- ...0221021062445_InitialMigration.Designer.cs | 113 ++++++++++++++++++ .../20221021062445_InitialMigration.cs | 71 +++++++++++ .../Migrations/TodoContextModelSnapshot.cs | 111 +++++++++++++++++ 3 files changed, 295 insertions(+) create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021062445_InitialMigration.Designer.cs create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021062445_InitialMigration.cs create mode 100644 azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021062445_InitialMigration.Designer.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021062445_InitialMigration.Designer.cs new file mode 100644 index 0000000..8bc2b67 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021062445_InitialMigration.Designer.cs @@ -0,0 +1,113 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TodoApp.DataAccess.DataModels; + +#nullable disable + +namespace TodoApp.DataAccess.Migrations +{ + [DbContext(typeof(TodoContext))] + [Migration("20221021062445_InitialMigration")] + partial class InitialMigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("CompletedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedUser") + .HasColumnType("bigint"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("IsCompleted") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("CreatedUser"); + + b.ToTable("Todo"); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("User"); + + b.HasData( + new + { + Id = 1L, + Email = "indunilw@99x.io", + FirstName = "Admin", + LastName = "Admin" + }); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => + { + b.HasOne("TodoApp.DataAccess.DataModels.User", "User") + .WithMany("ToDos") + .HasForeignKey("CreatedUser") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => + { + b.Navigation("ToDos"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021062445_InitialMigration.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021062445_InitialMigration.cs new file mode 100644 index 0000000..1e5bb72 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/20221021062445_InitialMigration.cs @@ -0,0 +1,71 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TodoApp.DataAccess.Migrations +{ + public partial class InitialMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "User", + columns: table => new + { + Id = table.Column(type: "bigint", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Email = table.Column(type: "nvarchar(max)", nullable: false), + FirstName = table.Column(type: "nvarchar(max)", nullable: false), + LastName = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_User", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Todo", + columns: table => new + { + Id = table.Column(type: "bigint", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: true), + IsCompleted = table.Column(type: "bit", nullable: false), + CreatedTime = table.Column(type: "datetime2", nullable: false), + CompletedTime = table.Column(type: "datetime2", nullable: true), + CreatedUser = table.Column(type: "bigint", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Todo", x => x.Id); + table.ForeignKey( + name: "FK_Todo_User_CreatedUser", + column: x => x.CreatedUser, + principalTable: "User", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.InsertData( + table: "User", + columns: new[] { "Id", "Email", "FirstName", "LastName" }, + values: new object[] { 1L, "indunilw@99x.io", "Admin", "Admin" }); + + migrationBuilder.CreateIndex( + name: "IX_Todo_CreatedUser", + table: "Todo", + column: "CreatedUser"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Todo"); + + migrationBuilder.DropTable( + name: "User"); + } + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs new file mode 100644 index 0000000..c081f3f --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.DataAccess/Migrations/TodoContextModelSnapshot.cs @@ -0,0 +1,111 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TodoApp.DataAccess.DataModels; + +#nullable disable + +namespace TodoApp.DataAccess.Migrations +{ + [DbContext(typeof(TodoContext))] + partial class TodoContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("CompletedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("CreatedUser") + .HasColumnType("bigint"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("IsCompleted") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("CreatedUser"); + + b.ToTable("Todo"); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("User"); + + b.HasData( + new + { + Id = 1L, + Email = "indunilw@99x.io", + FirstName = "Admin", + LastName = "Admin" + }); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.Todo", b => + { + b.HasOne("TodoApp.DataAccess.DataModels.User", "User") + .WithMany("ToDos") + .HasForeignKey("CreatedUser") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("TodoApp.DataAccess.DataModels.User", b => + { + b.Navigation("ToDos"); + }); +#pragma warning restore 612, 618 + } + } +}