Skip to content

Commit e267b65

Browse files
committed
Implementaton Model First for MVC and Razor Pages.
1 parent f52d208 commit e267b65

File tree

221 files changed

+153147
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

221 files changed

+153147
-2
lines changed

DotNet.EFCore.Sample.sln

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNet.EFCore.CodeFirstFlue
6262
EndProject
6363
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNet.EFCore.DatabaseFirst.Library", "DatabaseFirst\Library\DotNet.EFCore.DatabaseFirst.Library\DotNet.EFCore.DatabaseFirst.Library.csproj", "{CB8B89BA-BEFF-479A-9EEB-56126B627563}"
6464
EndProject
65+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNet.EFCore.ModelFirst.Library", "ModelFirst\Library\DotNet.EFCore.ModelFirst.Library\DotNet.EFCore.ModelFirst.Library.csproj", "{7F4FB994-DD06-4072-9D71-EFC201F7CF21}"
66+
EndProject
67+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNet.EFCore.ModelFirst.MvcApp", "ModelFirst\MVC\DotNet.EFCore.ModelFirst.MvcApp\DotNet.EFCore.ModelFirst.MvcApp.csproj", "{2F032603-876B-45F0-81EE-45D955F7C742}"
68+
EndProject
69+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNet.EFCore.ModelFirst.RazorApp", "ModelFirst\RazorPages\DotNet.EFCore.ModelFirst.RazorApp\DotNet.EFCore.ModelFirst.RazorApp.csproj", "{B2C69D13-3FD4-4A72-A598-DECF12E15212}"
70+
EndProject
6571
Global
6672
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6773
Debug|Any CPU = Debug|Any CPU
@@ -104,6 +110,18 @@ Global
104110
{CB8B89BA-BEFF-479A-9EEB-56126B627563}.Debug|Any CPU.Build.0 = Debug|Any CPU
105111
{CB8B89BA-BEFF-479A-9EEB-56126B627563}.Release|Any CPU.ActiveCfg = Release|Any CPU
106112
{CB8B89BA-BEFF-479A-9EEB-56126B627563}.Release|Any CPU.Build.0 = Release|Any CPU
113+
{7F4FB994-DD06-4072-9D71-EFC201F7CF21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
114+
{7F4FB994-DD06-4072-9D71-EFC201F7CF21}.Debug|Any CPU.Build.0 = Debug|Any CPU
115+
{7F4FB994-DD06-4072-9D71-EFC201F7CF21}.Release|Any CPU.ActiveCfg = Release|Any CPU
116+
{7F4FB994-DD06-4072-9D71-EFC201F7CF21}.Release|Any CPU.Build.0 = Release|Any CPU
117+
{2F032603-876B-45F0-81EE-45D955F7C742}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
118+
{2F032603-876B-45F0-81EE-45D955F7C742}.Debug|Any CPU.Build.0 = Debug|Any CPU
119+
{2F032603-876B-45F0-81EE-45D955F7C742}.Release|Any CPU.ActiveCfg = Release|Any CPU
120+
{2F032603-876B-45F0-81EE-45D955F7C742}.Release|Any CPU.Build.0 = Release|Any CPU
121+
{B2C69D13-3FD4-4A72-A598-DECF12E15212}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
122+
{B2C69D13-3FD4-4A72-A598-DECF12E15212}.Debug|Any CPU.Build.0 = Debug|Any CPU
123+
{B2C69D13-3FD4-4A72-A598-DECF12E15212}.Release|Any CPU.ActiveCfg = Release|Any CPU
124+
{B2C69D13-3FD4-4A72-A598-DECF12E15212}.Release|Any CPU.Build.0 = Release|Any CPU
107125
EndGlobalSection
108126
GlobalSection(SolutionProperties) = preSolution
109127
HideSolutionNode = FALSE
@@ -133,5 +151,8 @@ Global
133151
{3A07C435-3794-43E3-9A39-DAA73DCAE1E4} = {CD35F42C-1A08-4468-8B1D-3207183D64FE}
134152
{56B0F215-5F19-4294-A58D-CC890089C6EC} = {66F15477-03E0-4BA8-922D-52F99A894AF8}
135153
{CB8B89BA-BEFF-479A-9EEB-56126B627563} = {581028EC-D2D6-41DC-9523-F5C63185FE24}
154+
{7F4FB994-DD06-4072-9D71-EFC201F7CF21} = {FF31CC40-70D9-48A2-8170-4F885D4612AD}
155+
{2F032603-876B-45F0-81EE-45D955F7C742} = {D0A96BD4-5AC3-4497-85EE-4009E2B2D0BE}
156+
{B2C69D13-3FD4-4A72-A598-DECF12E15212} = {47382599-030C-496B-A873-F14F459007DC}
136157
EndGlobalSection
137158
EndGlobal
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace DotNet.EFCore.ModelFirst.Library.Constants
2+
{
3+
public static class WebConstants
4+
{
5+
// Projects
6+
public const string MvcProjectName = "DotNet MVC EFCore - Model First";
7+
public const string MvcProjectTitle = "MVC EFCore - Model First";
8+
public const string RazorProjectName = "DotNet Razor Pages EFCore - Model First";
9+
public const string RazorProjectTitle = "Razor Pages EFCore - Model First";
10+
11+
// Configurations
12+
public const string ConfigurationFileName = "appsettings.json";
13+
public const string ConnectionName = "DefaultConnection";
14+
15+
// Messages
16+
public const string InvalidEntity = "Invalid Entity";
17+
}
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

ModelFirst/Library/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div style="color:cornflowerblue">
2+
3+
# DotNet Entity Framework Core - Model First Approach Sample Library
4+
5+
</div>
6+
7+
Library Project holds the contents of Constants and Common Content.
8+
9+
### :fire: Happy Coding
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using DotNet.EFCore.ModelFirst.Library.Constants;
2+
using DotNet.EFCore.ModelFirst.MvcApp.Data;
3+
using DotNet.EFCore.ModelFirst.MvcApp.Models;
4+
using DotNet.EFCore.ModelFirst.MvcApp.Repository;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Newtonsoft.Json;
7+
using System.Diagnostics;
8+
9+
namespace DotNet.EFCore.ModelFirst.MvcApp.Controllers
10+
{
11+
/// <summary>
12+
/// Home Controller
13+
/// </summary>
14+
public class HomeController : Controller
15+
{
16+
/// <summary>
17+
/// Logger for Home Controller
18+
/// </summary>
19+
private readonly ILogger<HomeController> _logger;
20+
/// <summary>
21+
/// Entity Framework Core DB Context
22+
/// </summary>
23+
private readonly EFCoreModelFirstDatabaseContext _dbContext;
24+
25+
/// <summary>
26+
/// Constructor to Initialize class
27+
/// </summary>
28+
/// <param name="logger">logger</param>
29+
/// <param name="dbContext">DB context</param>
30+
public HomeController(ILogger<HomeController> logger, EFCoreModelFirstDatabaseContext dbContext)
31+
{
32+
_logger = logger;
33+
_dbContext = dbContext;
34+
}
35+
36+
/// <summary>
37+
/// Default Index View Action Method
38+
/// </summary>
39+
/// <returns>view action result</returns>
40+
public IActionResult Index()
41+
{
42+
// Initializing Response View Model
43+
var responseViewModel = new ResponseViewModel
44+
{
45+
// Invoking Repository Methods
46+
Departments = new GenericRepository<Department>(_dbContext).GetEntityData() ?? new List<Department>(),
47+
Employees = new GenericRepository<Employee>(_dbContext).GetEntityData() ?? new List<Employee>(),
48+
Skills = new GenericRepository<Skill>(_dbContext).GetEntityData() ?? new List<Skill>(),
49+
EmployeeSkills = new GenericRepository<EmployeeSkill>(_dbContext).GetEntityData() ?? new List<EmployeeSkill>()
50+
};
51+
52+
// Returning response view model to the view
53+
return View(responseViewModel);
54+
}
55+
56+
/// <summary>
57+
/// Controller Action method to perform entity creation.
58+
/// </summary>
59+
/// <param name="entityJson">entity JSON</param>
60+
/// <param name="entityName">entity name</param>
61+
/// <returns>json result of entity create operation</returns>
62+
[HttpPost]
63+
public JsonResult CreateEntity(string entityJson, string entityName)
64+
{
65+
var response = new SampleResponse { };
66+
67+
if (entityName.ToLower() == typeof(Employee).Name.ToLower())
68+
response.Data = new GenericRepository<Employee>(_dbContext).CreateEntity(JsonConvert.DeserializeObject<Employee>(entityJson));
69+
else if (entityName.ToLower() == typeof(Skill).Name.ToLower())
70+
response.Data = new GenericRepository<Skill>(_dbContext).CreateEntity(JsonConvert.DeserializeObject<Skill>(entityJson));
71+
else if (entityName.ToLower() == typeof(Department).Name.ToLower())
72+
response.Data = new GenericRepository<Department>(_dbContext).CreateEntity(JsonConvert.DeserializeObject<Department>(entityJson));
73+
else if (entityName.ToLower() == typeof(EmployeeSkill).Name.ToLower())
74+
response.Data = new GenericRepository<EmployeeSkill>(_dbContext).CreateEntity(JsonConvert.DeserializeObject<EmployeeSkill>(entityJson));
75+
else
76+
response.Data = WebConstants.InvalidEntity;
77+
78+
return Json(response);
79+
}
80+
81+
/// <summary>
82+
/// Controller Action method to perform entity updation/modification.
83+
/// </summary>
84+
/// <param name="entityJson">entity JSON</param>
85+
/// <param name="entityName">entity name</param>
86+
/// <returns>json result of entity update operation</returns>
87+
[HttpPut]
88+
public JsonResult UpdateEntity(string entityJson, string entityName)
89+
{
90+
var response = new SampleResponse { };
91+
92+
if (entityName.ToLower() == typeof(Employee).Name.ToLower())
93+
response.Data = new GenericRepository<Employee>(_dbContext).UpdateEntity(JsonConvert.DeserializeObject<Employee>(entityJson));
94+
else if (entityName.ToLower() == typeof(Skill).Name.ToLower())
95+
response.Data = new GenericRepository<Skill>(_dbContext).UpdateEntity(JsonConvert.DeserializeObject<Skill>(entityJson));
96+
else if (entityName.ToLower() == typeof(Department).Name.ToLower())
97+
response.Data = new GenericRepository<Department>(_dbContext).UpdateEntity(JsonConvert.DeserializeObject<Department>(entityJson));
98+
else if (entityName.ToLower() == typeof(EmployeeSkill).Name.ToLower())
99+
response.Data = new GenericRepository<EmployeeSkill>(_dbContext).UpdateEntity(JsonConvert.DeserializeObject<EmployeeSkill>(entityJson));
100+
else
101+
response.Data = WebConstants.InvalidEntity;
102+
103+
return Json(response);
104+
}
105+
106+
107+
/// <summary>
108+
/// Controller Action method to perform entity deletion.
109+
/// </summary>
110+
/// <param name="entityJson">entity JSON</param>
111+
/// <param name="entityName">entity name</param>
112+
/// <returns>json result of entity delete operation</returns>
113+
[HttpDelete]
114+
public JsonResult DeleteEntity(string entityJson, string entityName)
115+
{
116+
var response = new SampleResponse { };
117+
118+
if (entityName.ToLower() == typeof(Employee).Name.ToLower())
119+
response.Data = new GenericRepository<Employee>(_dbContext).DeleteEntity(JsonConvert.DeserializeObject<Employee>(entityJson));
120+
else if (entityName.ToLower() == typeof(Skill).Name.ToLower())
121+
response.Data = new GenericRepository<Skill>(_dbContext).DeleteEntity(JsonConvert.DeserializeObject<Skill>(entityJson));
122+
else if (entityName.ToLower() == typeof(Department).Name.ToLower())
123+
response.Data = new GenericRepository<Department>(_dbContext).DeleteEntity(JsonConvert.DeserializeObject<Department>(entityJson));
124+
else if (entityName.ToLower() == typeof(EmployeeSkill).Name.ToLower())
125+
response.Data = new GenericRepository<EmployeeSkill>(_dbContext).DeleteEntity(JsonConvert.DeserializeObject<EmployeeSkill>(entityJson));
126+
else
127+
response.Data = WebConstants.InvalidEntity;
128+
129+
return Json(response);
130+
}
131+
132+
/// <summary>
133+
/// Controller Action Method for Privacy view
134+
/// </summary>
135+
/// <returns>privacy view action result</returns>
136+
public IActionResult Privacy()
137+
{
138+
return View();
139+
}
140+
141+
/// <summary>
142+
/// Controller Action Method for Error View
143+
/// </summary>
144+
/// <returns>error view action result</returns>
145+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
146+
public IActionResult Error()
147+
{
148+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
149+
}
150+
}
151+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using DotNet.EFCore.ModelFirst.MvcApp.Models;
2+
3+
namespace DotNet.EFCore.ModelFirst.MvcApp.Data
4+
{
5+
/// <summary>
6+
/// Entity Framework Core Model First Database Initializer
7+
/// </summary>
8+
public static class EFCoreModelFirstDbInitializer
9+
{
10+
/// <summary>
11+
/// Initialize method to create database, tables and sample data.
12+
/// </summary>
13+
/// <param name="context">The DB Context</param>
14+
public static void Initialize(EFCoreModelFirstDatabaseContext context)
15+
{
16+
// Ensuring Database context exists when Database exists or newly created.
17+
context.Database.EnsureCreated();
18+
19+
if (context.Departments.Any())
20+
return; // Db has been seeded
21+
22+
// Departments Sample Data
23+
var departments = new Department[]
24+
{
25+
new Department { Id = new Guid("5A573A17-B74F-4F74-A8E1-D0184F4B0C90"), Name = "Engineering", IsActive = true },
26+
new Department { Id = new Guid("FBF88BF4-8D2B-46D4-9E52-C5B0E7E97279"), Name = "Designing", IsActive = true },
27+
new Department { Id = new Guid("0684877A-4DB5-4E64-9728-C44ACE259CFA"), Name = "Finance", IsActive = false },
28+
};
29+
30+
// Skills Sample Data
31+
var skills = new Skill[]
32+
{
33+
new Skill { Id = new Guid("69D0E326-2520-486C-99E2-385A34803579"), Name = "Designing", IsActive = true },
34+
new Skill { Id = new Guid("D765CBE3-0A1C-4A28-9F2E-56B36A50E8D2"), Name = "Management", IsActive = true },
35+
new Skill { Id = new Guid("3760C414-D917-467F-A482-9ED3887639DE"), Name = "Programming", IsActive = true },
36+
new Skill { Id = new Guid("C64489F0-2EE7-444F-A9FC-AD18550D8B6C"), Name = "Testing", IsActive = true },
37+
new Skill { Id = new Guid("3873D6CA-2B0C-44FC-B548-BDF22354E638"), Name = "Machine Assembly", IsActive = true },
38+
new Skill { Id = new Guid("0DE189F1-A036-40D8-AEA4-C0704C04EBE7"), Name = "Training", IsActive = false },
39+
new Skill { Id = new Guid("D811546C-B66B-46D3-9E23-F7441B4CD094"), Name = "Workflow", IsActive = false }
40+
};
41+
42+
// Employees Sample Data
43+
var employees = new Employee[]
44+
{
45+
new Employee { Id = new Guid("43DF727E-B8F7-4B9B-98C3-187E32844EC9"), Name = "John", Email = "john@abc.com", Phone = 8765432109, DepartmentId = new Guid("FBF88BF4-8D2B-46D4-9E52-C5B0E7E97279"), IsActive = true },
46+
new Employee { Id = new Guid("367DE4BD-A143-4196-8BAA-288705CD8963"), Name = "Arjun", Email = "arjun@abc.com", Phone = 9876543210, DepartmentId = new Guid("5A573A17-B74F-4F74-A8E1-D0184F4B0C90"), IsActive = true },
47+
new Employee { Id = new Guid("170528A9-3D66-4038-A16D-E6D10DE12824"), Name = "Ram", Email = "ram@abc.com", Phone = 7654321098, DepartmentId = new Guid("0684877A-4DB5-4E64-9728-C44ACE259CFA"), IsActive = false },
48+
new Employee { Id = new Guid("9B5C0AFD-9E08-4724-88F1-ECF47BC8BBB9"), Name = "Siri", Email = "siri@abc.com", Phone = 9123456780, DepartmentId = new Guid("5A573A17-B74F-4F74-A8E1-D0184F4B0C90"), IsActive = true }
49+
};
50+
51+
// Employee Skills Sample Data
52+
var employeeSkills = new EmployeeSkill[]
53+
{
54+
new EmployeeSkill { Id = new Guid("137E00EF-C9E4-4938-8E51-3938A954B2D2"), EmployeeId = new Guid("170528A9-3D66-4038-A16D-E6D10DE12824"), SkillId = new Guid("D811546C-B66B-46D3-9E23-F7441B4CD094") },
55+
new EmployeeSkill { Id = new Guid("A91B0E9D-4A18-4BA6-8D97-4C10AE43DAE8"), EmployeeId = new Guid("367DE4BD-A143-4196-8BAA-288705CD8963"), SkillId = new Guid("0DE189F1-A036-40D8-AEA4-C0704C04EBE7") },
56+
new EmployeeSkill { Id = new Guid("5953C3F5-E8AC-4007-9372-5B979F78F0AD"), EmployeeId = new Guid("9B5C0AFD-9E08-4724-88F1-ECF47BC8BBB9"), SkillId = new Guid("C64489F0-2EE7-444F-A9FC-AD18550D8B6C") },
57+
new EmployeeSkill { Id = new Guid("4710C4F0-8259-4DC4-9D45-8701214CCB88"), EmployeeId = new Guid("170528A9-3D66-4038-A16D-E6D10DE12824"), SkillId = new Guid("D765CBE3-0A1C-4A28-9F2E-56B36A50E8D2") },
58+
new EmployeeSkill { Id = new Guid("468DC805-C4EF-4287-B591-C70BE4C2F5D7"), EmployeeId = new Guid("43DF727E-B8F7-4B9B-98C3-187E32844EC9"), SkillId = new Guid("69D0E326-2520-486C-99E2-385A34803579") },
59+
new EmployeeSkill { Id = new Guid("ACD58021-9F5C-41AF-B9E9-C79AC37A11AD"), EmployeeId = new Guid("9B5C0AFD-9E08-4724-88F1-ECF47BC8BBB9"), SkillId = new Guid("3760C414-D917-467F-A482-9ED3887639DE") },
60+
new EmployeeSkill { Id = new Guid("5A63768A-6740-4929-9E40-E6CC246B3A25"), EmployeeId = new Guid("367DE4BD-A143-4196-8BAA-288705CD8963"), SkillId = new Guid("3760C414-D917-467F-A482-9ED3887639DE") },
61+
new EmployeeSkill { Id = new Guid("B0571C42-B526-47E0-B061-EC07880B7EFA"), EmployeeId = new Guid("367DE4BD-A143-4196-8BAA-288705CD8963"), SkillId = new Guid("D765CBE3-0A1C-4A28-9F2E-56B36A50E8D2") }
62+
};
63+
64+
// Adding Entities sample data to Context
65+
context.Departments.AddRange(departments);
66+
context.Skills.AddRange(skills);
67+
context.Employees.AddRange(employees);
68+
context.EmployeeSkills.AddRange(employeeSkills);
69+
70+
// Saving Changes to DB Context
71+
context.SaveChanges();
72+
}
73+
}
74+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using DotNet.EFCore.ModelFirst.Library.Constants;
2+
using DotNet.EFCore.ModelFirst.MvcApp.Models;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace DotNet.EFCore.ModelFirst.MvcApp.Data
6+
{
7+
/// <summary>
8+
/// Entity Framework Database Context
9+
/// </summary>
10+
public class EFCoreModelFirstDatabaseContext : DbContext
11+
{
12+
/// <summary>
13+
/// Constructor to initialize the Context Class
14+
/// </summary>
15+
public EFCoreModelFirstDatabaseContext()
16+
{
17+
}
18+
19+
/// <summary>
20+
/// Constructor to initialize the Context Class with DBContextOptions arguments.
21+
/// </summary>
22+
/// <param name="options">DB Context Options</param>
23+
public EFCoreModelFirstDatabaseContext(DbContextOptions<EFCoreModelFirstDatabaseContext> options)
24+
: base(options)
25+
{
26+
}
27+
28+
/// <summary>
29+
/// Department Class DbSet
30+
/// </summary>
31+
public virtual DbSet<Department> Departments { get; set; } = null!;
32+
/// <summary>
33+
/// Employee Class DbSet
34+
/// </summary>
35+
public virtual DbSet<Employee> Employees { get; set; } = null!;
36+
/// <summary>
37+
/// EmployeeSkill Class DbSet
38+
/// </summary>
39+
public virtual DbSet<EmployeeSkill> EmployeeSkills { get; set; } = null!;
40+
/// <summary>
41+
/// Skill Class DbSet
42+
/// </summary>
43+
public virtual DbSet<Skill> Skills { get; set; } = null!;
44+
45+
/// <summary>
46+
/// Overriding the Configuration of DBContext with sql server connection.
47+
/// </summary>
48+
/// <param name="optionsBuilder"></param>
49+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
50+
{
51+
if (!optionsBuilder.IsConfigured)
52+
{
53+
var builder = new ConfigurationBuilder().AddJsonFile(WebConstants.ConfigurationFileName, false, true);
54+
var configuration = builder.Build();
55+
var connectionString = configuration.GetConnectionString(WebConstants.ConnectionName);
56+
57+
optionsBuilder.UseSqlServer(connectionString, options => options.EnableRetryOnFailure());
58+
}
59+
}
60+
61+
/// <summary>
62+
/// Overriding the model builder and entity configurations.
63+
/// </summary>
64+
/// <param name="modelBuilder">Model Builder</param>
65+
protected override void OnModelCreating(ModelBuilder modelBuilder)
66+
{
67+
base.OnModelCreating(modelBuilder);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)