How To Create CoreApplication in Database First approach
Stetp
1
1 Add New Project
project name Demo.web
2 Add Class
Library Demo.Utilities
3 Add Class
Library Demo.BAL
4 Add Class
Library Demo.DAL
5 Add Class
Library Demo.Model
6 Add Class
Library Demo.SSDT
7 Add Class
Library Demo.Utilities
Stetp 2
Add
Project Dependencies.
After that select
Webproject (Demo.web)
BAL
Model
Utilities
After That Api Project Dependencies
BAL
Model
Utilities
After That BAL Project Dependencies
DAL
Model
Utilities
Step 3
Packages Installation
1 AWSSDK.Core(3.7.10.8)
2 MicrosoftEntityFrameworkCore.Design(5.0.0)
3 MicrosoftEntityFrameworkCore.SqlServer(5.0.0)
Dal Packages
Utility Packages
Add Resource File
Resources file re use message
And status message use for custome add Status
Class file
public class statusMsgs
{
public string SuccessMsg()
{
return "Successfull!";
}
public string SuccessCode()
{
return "200";
}
}
After That DAL Project Dependencies
After That Add Connection String in appseting.json file
"ConnectionStrings": {
"DefaultString": "Data Source=localhost\\SQLSEV2019;Initial
Catalog=CoreApplication;Persist Security Info=True;User ID=sa;Password=sa"
},
Step 4
Create Folder And class file
DemoEntites
Code(class file name)
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dotNetCoreMvcDemoProj.DAL.Enitites
{
public class DemoEntities:DbContext
{
public DemoEntities(DbContextOptions<DemoEntities>
options):base(options)
{
}
DbSet<tblEmployee> tblEmployee { get; set; }
}
}
tblEmployee.cs
Code
public class tblEmployee
{
[Key]
public int empID { get; set; }
public string EmpName { get; set; }
}
IRpository
class idemo(interface)
public interface IDemo
{
List<tblEmployee> GetAllEmployee();
bool
SaveEmployee(tblEmployee tblEmp);
}
Rpository
class Demo(class file)
public class Demo : IDemo
{
private DemoEntities
_demoEntities;
public Demo(DemoEntities demoEntities)
{
_demoEntities = demoEntities;
}
public
List<tblEmployee> GetAllEmployee()
{
var data =
_demoEntities.Set<tblEmployee>().ToList();
return data;
}
public bool
SaveEmployee(tblEmployee tblEmp)
{
tblEmployee emp = new tblEmployee();
emp.empID = tblEmp.empID;
emp.EmpName = tblEmp.EmpName;
_demoEntities.Add(emp);
var count= _demoEntities.SaveChanges();
return count > 0 ? true : false;
}
}
Business
layer class
IDemoServices.cs
public interface IDemoService
{
List<tblEmployee> GetAllEmployee();
bool
SaveEmployee(tblEmployee tblEmp);
}
DemoServices.cs
public class DemoService : IDemoService
{
private IDemo _demo;
public DemoService(IDemo demo)
{
_demo = demo;
}
public
List<tblEmployee> GetAllEmployee()
{
return _demo.GetAllEmployee();
}
public bool
SaveEmployee(tblEmployee tblEmp)
{
return _demo.SaveEmployee(tblEmp);
}
}
Startup Class
Startup class use form dependencies injection
1
connection string pass
2
servicess use
3
repository use
1=>
services.AddDbContext<DemoEntities>(options
=>options.UseSqlServer(Configuration.GetConnectionString("DefaultString")));
2 => services.AddScoped<IDemo, Demo>();
3 => services.AddScoped<IDemoService,
DemoService>();
Controller Function Use
public class HomeController : Controller
{
private readonly
ILogger<HomeController> _logger;
private IDemoService
_demoService; this is use
service file
public HomeController(ILogger<HomeController>
logger, IDemoService
demoService)
{
_logger = logger;
_demoService = demoService; constructor use
}
public IActionResult Index()
{
var data = _demoService.GetAllEmployee();
if (data != null && data.Count > 0)
{
ViewBag.EmpData = data;
}
return View();
}
[HttpGet]
public ActionResult AddEmp(tblEmployee
tbl)
{
tblEmployee emp = new tblEmployee();
emp.empID = tbl.empID;
emp.EmpName = tbl.EmpName;
var status=_demoService.SaveEmployee(emp);
//ViewBag.status = status;
//_statusMsgs.SuccessMsg();
//_statusMsgs.SuccessCode();
return Json(status);
}
public IActionResult
Privacy()
{
return View();
}
}
Html Page
@model dotNetCoreMvcDemoProj.DAL.Enitites.tblEmployee
@{
ViewData["Title"] = "Index";
Layout = "_Layout";
}
@*<link
rel="stylesheet"
href="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<link rel="stylesheet"
href="https://cdn.datatables.net/plug-ins/9dcbecd42ad/integration/jqueryui/dataTables.jqueryui.css">*@
<h1>Index</h1>
<h4>tblEmployee</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Index">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
@*<div class="form-group">
<label
asp-for="empID" class="control-label"></label>
<input
asp-for="empID" class="form-control" />
<span
asp-validation-for="empID"
class="text-danger"></span>
</div>*@
<div class="form-group">
<label asp-for="EmpName" class="control-label"></label>
<input type="text" id="empname" class="form-control" />
<input type="hidden" id="empname1" class="form-control" />
<span asp-validation-for="EmpName" class="text-danger"></span>
</div>
<div class="form-group">
<input type="button" id="saveEmp" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<table @*class="table
table-bordered"*@ id="table_id">
<thead>
<tr>
<td>Emp ID</td>
<td>Emp Name</td>
<td>Action</td>
</tr>
</thead>
<tbody>
@if (ViewBag.EmpData != null)
{
foreach (var item in ViewBag.EmpData)
{
<tr>
<td>@item.empID</td>
<td>@item.EmpName</td>
<td><input type="button" value="Edit" class="getData" /></td>
<td><input type="button" value="Delete" /></td>
</tr>
}
}
</tbody>
</table>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
<script src="~/custom/AddEmployee.js"></script>
@*<script
src="https://cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>*@
@*<script
src="https://cdn.datatables.net/plug-ins/9dcbecd42ad/integration/jqueryui/dataTables.jqueryui.js">
</script>*@
@*<script>
$(document).ready(function () {
$('#table_id').DataTable();
});
</script>*@
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
<script>
$("#saveEmp").click(function () {
var tbl = { empID: $("#empname1").val(), EmpName: $('#empname').val()};
//tbl.EmpName
= $('#empname').val();
console.log(tbl)
$.ajax({
@*url:
'@Url.Action("AddEmp", "Home")',*@
url: '/Home/AddEmp',
/* data: JSON.stringify(tbl),*/
data: tbl,
type: "GET",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
if (data) {
alert("emp saved
successfully!")
}
else {
alert("emp not
saved!")
}
window.href.reload();
},
error: function () {
}
});
});
$(document).on('click', '.getData', function () {
$('#saveEmp').val("Update");
$("#empname1").val($(this).closest('tr').children("td:eq(0)").text());
$("#empname").val($(this).closest('tr').children("td:eq(1)").text());
});
</script>
Jquery Add
Function
Insert
Grid View row get function
$(document).on('click', '.getData', function () {
$("#empname1").val($(this).closest('tr').children("td:eq(0)").text());
$("#empname").val($(this).closest('tr').children("td:eq(1)").text());
});
No comments:
Post a Comment