Commit bf2a84eb authored by boncho vylkov's avatar boncho vylkov

add new branch with entend of the db models

parent 16399631
......@@ -5,6 +5,7 @@ namespace MvcCoreTemplate.Common
public class GlobalConstants
{
public const string AdministratorRoleName = "Administrator";
public const string AdministratorUserName = "Admin@abv.bg";
public const string AppName = "MvcCoreTemplate";
}
......
namespace MvcCoreTemplate.Data.Common.Models
{
using System;
using System.ComponentModel.DataAnnotations;
public abstract class BaseModel<TKey> : IAuditInfo, IDeletableEntity
{
[Key]
public TKey Id { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public bool IsDeleted { get; set; }
public DateTime? DeletedOn { get; set; }
}
}
namespace MvcCoreTemplate.Data.Common.Models
{
using System;
public interface IAuditInfo
{
DateTime CreatedOn { get; set; }
DateTime? ModifiedOn { get; set; }
}
}
namespace MvcCoreTemplate.Data.Common.Models
{
using System;
public interface IDeletableEntity
{
bool IsDeleted { get; set; }
DateTime? DeletedOn { get; set; }
}
}
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
......
namespace MvcCoreTemplate.Data.Models
{
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using MvcCoreTemplate.Data.Common.Models;
using System;
public class ApplicationRole : IdentityRole, IAuditInfo, IDeletableEntity
{
public ApplicationRole()
: this(null)
{
}
public ApplicationRole(string name)
: base(name)
{
this.CreatedOn = DateTime.UtcNow;
}
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public bool IsDeleted { get; set; }
public DateTime? DeletedOn { get; set; }
}
}
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;
namespace MvcCoreTemplate.Data.Models
namespace MvcCoreTemplate.Data.Models
{
public class ApplicationUser : IdentityUser
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using MvcCoreTemplate.Data.Common.Models;
using System;
public class ApplicationUser : IdentityUser, IAuditInfo, IDeletableEntity
{
}
public ApplicationUser()
{
this.CreatedOn = DateTime.UtcNow;
}
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public bool IsDeleted { get; set; }
public DateTime? DeletedOn { get; set; }
}
}
namespace MvcCoreTemplate.Data.Models
{
using MvcCoreTemplate.Data.Common.Models;
public class Feature : BaseModel<int>
{
public string Name { get; set; }
public string Description { get; set; }
}
}
......@@ -8,4 +8,8 @@
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MvcCoreTemplate.Data.Common\MvcCoreTemplate.Data.Common.csproj" />
</ItemGroup>
</Project>
\ No newline at end of file
......@@ -5,10 +5,11 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using MvcCoreTemplate.Data.Models;
using MvcCoreTemplate.Data.Common.Models;
namespace MvcCoreTemplate.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
......@@ -22,5 +23,33 @@ namespace MvcCoreTemplate.Data
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
public DbSet<Feature> Features { get; set; }
public override int SaveChanges()
{
this.ApplyAuditInfoRules();
return base.SaveChanges();
}
private void ApplyAuditInfoRules()
{
var changedEntries = this.ChangeTracker.Entries()
.Where(
e => e.Entity is IAuditInfo && (e.State == EntityState.Added || e.State == EntityState.Modified));
foreach (var entry in changedEntries)
{
var entity = (IAuditInfo)entry.Entity;
if (entry.State == EntityState.Added && entity.CreatedOn == default(DateTime))
{
entity.CreatedOn = DateTime.UtcNow;
}
else
{
entity.ModifiedOn = DateTime.UtcNow;
}
}
}
}
}
......@@ -8,8 +8,8 @@ using MvcCoreTemplate.Data;
namespace MvcCoreTemplate.Data.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20170425144347_InitialMigration")]
partial class InitialMigration
[Migration("20170426090320_InitialExtendUsers")]
partial class InitialExtendUsers
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
......@@ -17,29 +17,6 @@ namespace MvcCoreTemplate.Data.Migrations
.HasAnnotation("ProductVersion", "1.1.1")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex");
b.ToTable("AspNetRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
......@@ -124,6 +101,37 @@ namespace MvcCoreTemplate.Data.Migrations
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.ApplicationRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<DateTime>("CreatedOn");
b.Property<DateTime?>("DeletedOn");
b.Property<bool>("IsDeleted");
b.Property<DateTime?>("ModifiedOn");
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex");
b.ToTable("AspNetRoles");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.ApplicationUser", b =>
{
b.Property<string>("Id")
......@@ -134,15 +142,23 @@ namespace MvcCoreTemplate.Data.Migrations
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<DateTime>("CreatedOn");
b.Property<DateTime?>("DeletedOn");
b.Property<string>("Email")
.HasMaxLength(256);
b.Property<bool>("EmailConfirmed");
b.Property<bool>("IsDeleted");
b.Property<bool>("LockoutEnabled");
b.Property<DateTimeOffset?>("LockoutEnd");
b.Property<DateTime?>("ModifiedOn");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256);
......@@ -174,9 +190,31 @@ namespace MvcCoreTemplate.Data.Migrations
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.Feature", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("CreatedOn");
b.Property<DateTime?>("DeletedOn");
b.Property<string>("Description");
b.Property<bool>("IsDeleted");
b.Property<DateTime?>("ModifiedOn");
b.Property<string>("Name");
b.HasKey("Id");
b.ToTable("Features");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole")
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationRole")
.WithMany("Claims")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
......@@ -200,7 +238,7 @@ namespace MvcCoreTemplate.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserRole<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole")
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationRole")
.WithMany("Users")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
......
......@@ -5,36 +5,40 @@ using Microsoft.EntityFrameworkCore.Metadata;
namespace MvcCoreTemplate.Data.Migrations
{
public partial class InitialMigration : Migration
public partial class InitialExtendUsers : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "AspNetRoles",
name: "AspNetUserTokens",
columns: table => new
{
Id = table.Column<string>(nullable: false),
ConcurrencyStamp = table.Column<string>(nullable: true),
Name = table.Column<string>(maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(maxLength: 256, nullable: true)
UserId = table.Column<string>(nullable: false),
LoginProvider = table.Column<string>(nullable: false),
Name = table.Column<string>(nullable: false),
Value = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetRoles", x => x.Id);
table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name });
});
migrationBuilder.CreateTable(
name: "AspNetUserTokens",
name: "AspNetRoles",
columns: table => new
{
UserId = table.Column<string>(nullable: false),
LoginProvider = table.Column<string>(nullable: false),
Name = table.Column<string>(nullable: false),
Value = table.Column<string>(nullable: true)
Id = table.Column<string>(nullable: false),
ConcurrencyStamp = table.Column<string>(nullable: true),
CreatedOn = table.Column<DateTime>(nullable: false),
DeletedOn = table.Column<DateTime>(nullable: true),
IsDeleted = table.Column<bool>(nullable: false),
ModifiedOn = table.Column<DateTime>(nullable: true),
Name = table.Column<string>(maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(maxLength: 256, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name });
table.PrimaryKey("PK_AspNetRoles", x => x.Id);
});
migrationBuilder.CreateTable(
......@@ -44,10 +48,14 @@ namespace MvcCoreTemplate.Data.Migrations
Id = table.Column<string>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false),
ConcurrencyStamp = table.Column<string>(nullable: true),
CreatedOn = table.Column<DateTime>(nullable: false),
DeletedOn = table.Column<DateTime>(nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
IsDeleted = table.Column<bool>(nullable: false),
LockoutEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
ModifiedOn = table.Column<DateTime>(nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
PasswordHash = table.Column<string>(nullable: true),
......@@ -62,6 +70,24 @@ namespace MvcCoreTemplate.Data.Migrations
table.PrimaryKey("PK_AspNetUsers", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Features",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
CreatedOn = table.Column<DateTime>(nullable: false),
DeletedOn = table.Column<DateTime>(nullable: true),
Description = table.Column<string>(nullable: true),
IsDeleted = table.Column<bool>(nullable: false),
ModifiedOn = table.Column<DateTime>(nullable: true),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Features", x => x.Id);
});
migrationBuilder.CreateTable(
name: "AspNetRoleClaims",
columns: table => new
......@@ -148,12 +174,6 @@ namespace MvcCoreTemplate.Data.Migrations
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
table: "AspNetRoles",
column: "NormalizedName",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_AspNetRoleClaims_RoleId",
table: "AspNetRoleClaims",
......@@ -174,6 +194,12 @@ namespace MvcCoreTemplate.Data.Migrations
table: "AspNetUserRoles",
column: "RoleId");
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
table: "AspNetRoles",
column: "NormalizedName",
unique: true);
migrationBuilder.CreateIndex(
name: "EmailIndex",
table: "AspNetUsers",
......@@ -203,6 +229,9 @@ namespace MvcCoreTemplate.Data.Migrations
migrationBuilder.DropTable(
name: "AspNetUserTokens");
migrationBuilder.DropTable(
name: "Features");
migrationBuilder.DropTable(
name: "AspNetRoles");
......
......@@ -16,29 +16,6 @@ namespace MvcCoreTemplate.Data.Migrations
.HasAnnotation("ProductVersion", "1.1.1")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex");
b.ToTable("AspNetRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
......@@ -123,6 +100,37 @@ namespace MvcCoreTemplate.Data.Migrations
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.ApplicationRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<DateTime>("CreatedOn");
b.Property<DateTime?>("DeletedOn");
b.Property<bool>("IsDeleted");
b.Property<DateTime?>("ModifiedOn");
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex");
b.ToTable("AspNetRoles");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.ApplicationUser", b =>
{
b.Property<string>("Id")
......@@ -133,15 +141,23 @@ namespace MvcCoreTemplate.Data.Migrations
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<DateTime>("CreatedOn");
b.Property<DateTime?>("DeletedOn");
b.Property<string>("Email")
.HasMaxLength(256);
b.Property<bool>("EmailConfirmed");
b.Property<bool>("IsDeleted");
b.Property<bool>("LockoutEnabled");
b.Property<DateTimeOffset?>("LockoutEnd");
b.Property<DateTime?>("ModifiedOn");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256);
......@@ -173,9 +189,31 @@ namespace MvcCoreTemplate.Data.Migrations
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.Feature", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("CreatedOn");
b.Property<DateTime?>("DeletedOn");
b.Property<string>("Description");
b.Property<bool>("IsDeleted");
b.Property<DateTime?>("ModifiedOn");
b.Property<string>("Name");
b.HasKey("Id");
b.ToTable("Features");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole")
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationRole")
.WithMany("Claims")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
......@@ -199,7 +237,7 @@ namespace MvcCoreTemplate.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserRole<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole")
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationRole")
.WithMany("Users")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
......
......@@ -14,6 +14,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MvcCoreTemplate.Common\MvcCoreTemplate.Common.csproj" />
<ProjectReference Include="..\MvcCoreTemplate.Data.Models\MvcCoreTemplate.Data.Models.csproj" />
</ItemGroup>
......
namespace MvcCoreTemplate.Data.Seeding
{
using System;
using System.Linq;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using MvcCoreTemplate.Data.Models;
using MvcCoreTemplate.Common;
public class ApplicationDbContextSeeder
{
public void Seed(ApplicationDbContext dbContext, IServiceProvider serviceProvider)
{
if (dbContext == null)
{
throw new ArgumentNullException(nameof(dbContext));
}
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
var roleManager = serviceProvider.GetRequiredService<RoleManager<ApplicationRole>>();
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
this.Seed(dbContext, roleManager, userManager);
}
public void Seed(ApplicationDbContext dbContext, RoleManager<ApplicationRole> roleManager, UserManager<ApplicationUser> userManager)
{
this.SeedRoles(roleManager);
this.SeedAdmin(userManager);
}
private void SeedAdmin(UserManager<ApplicationUser> userManager)
{
this.SeedAdmin(GlobalConstants.AdministratorUserName, userManager);
}
private void SeedAdmin(string administratorUserName, UserManager<ApplicationUser> userManager)
{
var admin = userManager.FindByNameAsync(administratorUserName).GetAwaiter().GetResult();
if (admin==null)
{
var user = new ApplicationUser { UserName = GlobalConstants.AdministratorUserName, Email = GlobalConstants.AdministratorUserName };
var resultUser = userManager.CreateAsync(user,GlobalConstants.AdministratorUserName+"1qaz").GetAwaiter().GetResult();
userManager.AddToRoleAsync(user, GlobalConstants.AdministratorRoleName).GetAwaiter().GetResult();
}
}
private void SeedRoles(RoleManager<ApplicationRole> roleManager)
{
this.SeedRole(GlobalConstants.AdministratorRoleName, roleManager);
}
private void SeedRole(string roleName, RoleManager<ApplicationRole> roleManager)
{
var role = roleManager.FindByNameAsync(roleName).GetAwaiter().GetResult();
if (role == null)
{
var result = roleManager.CreateAsync(new ApplicationRole(roleName)).GetAwaiter().GetResult();
if (!result.Succeeded)
{
throw new Exception(string.Join(Environment.NewLine, result.Errors.Select(e => e.Description)));
}
}
}
}
}
......@@ -9,6 +9,7 @@ using MvcCoreTemplate.Data.Models;
using MvcCoreTemplate.Data;
using MvcCoreTemplate.Services.Web;
using MvcCoreTemplate.Services.Data;
using MvcCoreTemplate.Data.Seeding;
namespace MvcCoreTemplate.Web