Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using Syncfusion.EJ2.Base;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Grid_EF.Controllers
{
[ApiController]
public class GridController : ControllerBase
{
string ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\NithyaSivaprakasam\Downloads\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30";

/// <summary>
/// Processes the DataManager request to perform searching, filtering, sorting, and paging operations.
/// </summary>
/// <param name="DataManagerRequest">Contains the details of the data operation requested.</param>
/// <returns>Returns a JSON object along with the total record count.</returns>
[HttpPost]
[Route("api/[controller]")]
public object Post([FromBody] DataManagerRequest DataManagerRequest)
{
// Retrieve data from the data source (e.g., database).
IQueryable<Orders> DataSource = GetOrderData().AsQueryable();

// Initialize QueryableOperation instance.
QueryableOperation queryableOperation = new QueryableOperation();

// Handling searching operation.
if (DataManagerRequest.Search != null && DataManagerRequest.Search.Count > 0)
{
DataSource = queryableOperation.PerformSearching(DataSource, DataManagerRequest.Search);
//Add custom logic here if needed and remove above method.
}
// Handling filtering operation.
if (DataManagerRequest.Where != null && DataManagerRequest.Where.Count > 0)
{
foreach (WhereFilter condition in DataManagerRequest.Where)
{
foreach (WhereFilter predicate in condition.predicates)
{
DataSource = queryableOperation.PerformFiltering(DataSource, DataManagerRequest.Where, predicate.Operator);
//Add custom logic here if needed and remove above method.
}
}
}

// Handling sorting operation.
if (DataManagerRequest.Sorted != null && DataManagerRequest.Sorted.Count > 0)
{
DataSource = queryableOperation.PerformSorting(DataSource, DataManagerRequest.Sorted);
//Add custom logic here if needed and remove above method.
}

// Get the total count of records.
int totalRecordsCount = DataSource.Count();

// Handling paging operation.
if (DataManagerRequest.Skip != 0)
{
DataSource = queryableOperation.PerformSkip(DataSource, DataManagerRequest.Skip);
//Add custom logic here if needed and remove above method.
}
if (DataManagerRequest.Take != 0)
{
DataSource = queryableOperation.PerformTake(DataSource, DataManagerRequest.Take);
//Add custom logic here if needed and remove above method.
}

// Return data based on the request.
return new { result = DataSource, count = totalRecordsCount };
}

/// <summary>
/// Retrieves the order data from the database.
/// </summary>
/// <returns>Returns a list of orders fetched from the database.</returns>
[HttpGet]
[Route("api/[controller]")]
public List<Orders> GetOrderData()
{
using (OrderDbContext Context = new OrderDbContext(ConnectionString))
{
// Retrieve orders from the orders DbSet and convert to list asynchronously.
List<Orders> orders = Context.Orders.ToList();
return orders;
}
}

// Create a class that inherits from DbContext(Entity Framework Core).
public class OrderDbContext : DbContext
{
//Declare a private variable to store the connection string.
private readonly string _ConnectionString;

//Define a constructor that accepts a connection string.
public OrderDbContext(string ConnectionString)
{
//Store the provided connection string.
_ConnectionString = ConnectionString;
}

//Override the onConfiguring method to tell EF Core to use SQL server.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Use the connection string to configure the database connection.
optionsBuilder.UseSqlServer(_ConnectionString);
}

// Define a DbSet to represent the orders table in the database.
public DbSet<Orders> Orders { get; set; }
}

/// <summary>
/// Inserts a new data item into the data collection.
/// </summary>
/// <param name="value">It contains the new record detail which is need to be inserted.</param>
/// <returns>Returns void.</returns>
[HttpPost]
[Route("api/[controller]/Insert")]
public void Insert([FromBody] CRUDModel<Orders> value)
{
using (OrderDbContext Context = new OrderDbContext(ConnectionString))
{
// Add the provided order to the orders DbSet.
Context.Orders.Add(value.value);

// Save changes to the database.
Context.SaveChanges();
}

//Add custom logic here if needed and remove above method.
}

/// <summary>
/// Update a existing data item from the data collection.
/// </summary>
/// <param name="value">It contains the updated record detail which is need to be updated.</param>
/// <returns>Returns void.</returns>
[HttpPost]
[Route("api/[controller]/Update")]
public void Update([FromBody] CRUDModel<Orders> value)
{
using (OrderDbContext Context = new OrderDbContext(ConnectionString))
{
Orders existingOrder = Context.Orders.Find(value.value.OrderID);
if (existingOrder != null)
{
// Update the existing order with the new values.
Context.Entry(existingOrder).CurrentValues.SetValues(value.value);

// Save changes to the database.
Context.SaveChanges();
}
}

//Add custom logic here if needed and remove above method.
}

/// <summary>
/// Remove a specific data item from the data collection.
/// </summary>
/// <param name="value">It contains the specific record detail which is need to be removed.</param>
/// <return>Returns void.</return>
[HttpPost]
[Route("api/[controller]/Remove")]
public void Remove([FromBody] CRUDModel<Orders> value)
{
int OrderId = Convert.ToInt32(value.key.ToString());
using (OrderDbContext Context = new OrderDbContext(ConnectionString))
{
Orders Order = Context.Orders.Find(OrderId);
if (Order != null)
{
// Remove the order from the orders DbSet.
Context.Orders.Remove(Order);

// Save changes to the database.
Context.SaveChanges();
}
}

//Add custom logic here if needed and remove above method.
}

/// <summary>
/// Batch update (Insert, Update, and Delete) a collection of data items from the data collection.
/// </summary>
/// <param name="value">The set of information along with details about the CRUD actions to be executed from the database.</param>
/// <returns>Returns void.</returns>
[HttpPost]
[Route("api/[controller]/BatchUpdate")]
public IActionResult BatchUpdate([FromBody] CRUDModel<Orders> value)
{
using (OrderDbContext Context = new OrderDbContext(ConnectionString))
{
if (value.changed != null && value.changed.Count > 0)
{
foreach (Orders Record in (IEnumerable<Orders>)value.changed)
{
// Update the changed records.
Context.Orders.UpdateRange(Record);
}
}

if (value.added != null && value.added.Count > 0)
{
foreach (Orders Record in (IEnumerable<Orders>)value.added)
{
foreach (Orders order in value.added)
{
// This ensures EF does not try to insert OrderID.
order.OrderID = default;
}
// Add new records.
Context.Orders.AddRange(value.added);
}
}

if (value.deleted != null && value.deleted.Count > 0)
{
foreach (Orders Record in (IEnumerable<Orders>)value.deleted)
{
// Find and delete the records.
Orders ExistingOrder = Context.Orders.Find(Record.OrderID);
if (ExistingOrder != null)
{
Context.Orders.Remove(ExistingOrder);
}
}
}

// Save changes to the database.
Context.SaveChanges();
}
return new JsonResult(value);
}


public class CRUDModel<T> where T : class
{
public string? action { get; set; }
public string? keyColumn { get; set; }
public object? key { get; set; }
public T? value { get; set; }
public List<T>? added { get; set; }
public List<T>? changed { get; set; }
public List<T>? deleted { get; set; }
public IDictionary<string, object>? @params { get; set; }
}

public class Orders
{
[Key]
public int? OrderID { get; set; }
public string? CustomerID { get; set; }
public int? EmployeeID { get; set; }
public decimal Freight { get; set; }
public string? ShipCity { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.3" />
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="29.1.35" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grid_EntityFrameWork", "Grid_EntityFrameWork.csproj", "{D56A855D-3C13-48FB-9AF4-86590FD014E7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D56A855D-3C13-48FB-9AF4-86590FD014E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D56A855D-3C13-48FB-9AF4-86590FD014E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D56A855D-3C13-48FB-9AF4-86590FD014E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D56A855D-3C13-48FB-9AF4-86590FD014E7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions Binding SQL database using EF and CustomAdaptor/Pages/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@page
@model ErrorModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Grid_EF.Pages
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}

}
Loading