CSC378 ยท 2081 ยท 10 marks
List some advantages of Entity Framework over ADO.NET. Assume a database named 'IOST' with a table FACULTY(Course_Name, CourseID, No_of_Semester, fee). Insert some records using ADO.NET and retrive the fee of records having name CSIT.
List some advantages of Entity Framework over ADO.NET. Assume a database named 'IOST' with a table FACULTY(Course_Name, CourseID, No_of_Semester, fee). Insert some records using ADO.NET and retrive the fee of records having name CSIT.[10]
Quick answer
Entity Framework (EF) is an Object/Relational Mapping (O/RM) framework that is an enhancement to ADO.NET giving developers an automated mechanism for accessing and storing data in the database.
Entity Framework vs ADO.NET and ADO.NET Database Operations
Part 1: Advantages of Entity Framework over ADO.NET
| # | Advantage of Entity Framework | ADO.NET Limitation |
|---|---|---|
| 1 | Automated Data Access: EF provides automated mechanism for accessing and storing data; no need to write raw SQL queries manually | ADO.NET requires writing explicit SQL queries for every operation |
| 2 | O/RM Support: Maps database tables directly to C# classes (domain objects), so developers work with objects instead of tables | ADO.NET works at a lower level with DataReaders, DataSets, and manual mapping |
| 3 | Cross-Platform: EF Core is open-source, lightweight, extensible and cross-platform | ADO.NET has limited cross-platform support |
| 4 | Two Development Approaches: Supports Code-First and Database-First approaches giving flexibility in design | ADO.NET does not support code-first or migration-based schema generation |
| 5 | Migration Support: In Code-First, EF Core API creates the database and tables using migration based on conventions and configuration in domain classes | ADO.NET requires manual creation and management of database schema |
| 6 | Less Boilerplate Code: CRUD operations require significantly fewer lines of code using EF | ADO.NET requires verbose code (connection, command, reader, etc.) for every operation |
| 7 | Domain Driven Design (DDD): Code-First approach is useful in DDD, making it suitable for enterprise applications | ADO.NET is not aligned with DDD principles |
| 8 | Works with .NET Core and .NET 4.5+: EF Core is intended for .NET Core but also supports standard .NET 4.5+ applications | ADO.NET is primarily tied to the traditional .NET framework |
Part 2: ADO.NET - Insert Records and Retrieve Fee for CSIT
Database Setup Assumption
- Database Name:
IOST - Table:
FACULTY(Course_Name, CourseID, No_of_Semester, fee)
Step 1: Create the Table in SQL Server (for reference)
CREATE DATABASE IOST;
USE IOST;
CREATE TABLE FACULTY (
Course_Name VARCHAR(100),
CourseID INT PRIMARY KEY,
No_of_Semester INT,
fee DECIMAL(10, 2)
);
Step 2: Full ADO.NET C# Code - Insert Records and Retrieve Fee for CSIT
using System;
using System.Data;
using System.Data.SqlClient;
namespace IOSTFacultyApp
{
class Program
{
// Connection string pointing to IOST database
static string connectionString =
"Data Source=.;Initial Catalog=IOST;Integrated Security=True";
static void Main(string[] args)
{
// Step 1: Insert records into FACULTY table
InsertFaculty("CSIT", 1, 8, 75000.00m);
InsertFaculty("BCA", 2, 6, 55000.00m);
InsertFaculty("CSIT", 3, 8, 80000.00m);
InsertFaculty("BIT", 4, 8, 60000.00m);
Console.WriteLine("Records inserted successfully.\n");
// Step 2: Retrieve fee of records where Course_Name = 'CSIT'
RetrieveCSITFee();
Console.ReadLine();
}
// -------------------------------------------------------
// Method to INSERT a record into FACULTY table
// -------------------------------------------------------
static void InsertFaculty(string courseName, int courseID,
int noOfSemester, decimal fee)
{
// SQL INSERT query using parameterized query to prevent SQL injection
string insertQuery = @"INSERT INTO FACULTY
(Course_Name, CourseID, No_of_Semester, fee)
VALUES
(@CourseName, @CourseID, @NoOfSemester, @Fee)";
// Using block ensures connection is closed automatically
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(insertQuery, con))
{
// Add parameters to avoid SQL injection
cmd.Parameters.AddWithValue("@CourseName", courseName);
cmd.Parameters.AddWithValue("@CourseID", courseID);
cmd.Parameters.AddWithValue("@NoOfSemester", noOfSemester);
cmd.Parameters.AddWithValue("@Fee", fee);
// Open connection
con.Open();
// Execute the INSERT command
int rowsAffected = cmd.ExecuteNonQuery();
Console.WriteLine($"Inserted: {courseName} | Rows Affected: {rowsAffected}");
}
// Connection is closed automatically at end of using block
}
}
// -------------------------------------------------------
// Method to RETRIEVE fee of FACULTY records where
// Course_Name = 'CSIT'
// -------------------------------------------------------
static void RetrieveCSITFee()
{
// SQL SELECT query with WHERE clause to filter CSIT records
string selectQuery = @"SELECT Course_Name, CourseID,
No_of_Semester, fee
FROM FACULTY
WHERE Course_Name = @CourseName";
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(selectQuery, con))
{
// Parameterized filter for Course_Name
cmd.Parameters.AddWithValue("@CourseName, "CSIT");
con.Open();
// Execute the query and read results row by row
SqlDataReader reader = cmd.ExecuteReader();
Console.WriteLine("Fee details for CSIT:");
Console.WriteLine("Course_Name | CourseID | No_of_Semester | Fee");
while (reader.Read())
{
Console.WriteLine(
$"{reader["Course_Name"]} | {reader["CourseID"]} | " +
$"{reader["No_of_Semester"]} | {reader["fee"]}"
);
}
reader.Close();
}
// Connection is closed automatically at end of using block
}
}
}
}
The InsertFaculty method uses a parameterized INSERT with AddWithValue for every field, guarding against SQL injection while adding the four faculty records (two of which are CSIT). The RetrieveCSITFee method then runs a parameterized SELECT ... WHERE Course_Name = @CourseName filtered to "CSIT", opens a SqlDataReader, and loops over reader.Read() to print each matching row's course name, course ID, number of semesters, and fee, giving exactly the fee details for the CSIT records that were inserted.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.