Author - Satwinder Kaur Post Views - 41 views

Active Record pattern with an example in C#

In C# and Entity Framework, the Active Record pattern represents database tables as classes and combines both data access and business logic within these classes. Each database table is mapped to a corresponding class, and instances of these classes represent rows in the table. These classes encapsulate the database operations, allowing developers to interact with the database using object-oriented principles.

Here’s a definition using Entity Framework and an example.

Definition:

Active Record Pattern: In C# with Entity Framework, the Active Record pattern is an architectural design pattern where each database table is represented by a class, and this class handles CRUD (Create, Read, Update, Delete) operations for the associated table. The class encapsulates both data access logic and business logic, making it easier to work with database records as objects.

Example using the Entity Framework:

Suppose we have a database table named “Students” with the following columns: StudentId, FirstName, LastName, and Age. We can create an Active Record class for this table using the Entity Framework.

1. Create a Model Class:

using System.ComponentModel.DataAnnotations;
public class Student
{
[Key] public int StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}

Set up an Entity Framework:

We need to set up an Entity Framework in your project by installing the Entity Framework package via NuGet Package Manager and configuring the DbContext.


using Microsoft.EntityFrameworkCore;
public class SchoolDbContext : DbContext
{
public DbSet Students { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Configure the database connection string here
optionsBuilder.UseSqlServer("DB_Connection_String");
}
}

CRUD Operations:
Now, we can perform CRUD operations using the Active Record pattern:

Create a Student

using (var context = new SchoolDbContext())
{
var newStudent = new Student
{
FirstName = "John",
LastName = "Doe",
Age = 20
};
context.Students.Add(newStudent);
context.SaveChanges();
}

Read a Student:

using (var context = new SchoolDbContext())
{
var student = context.Students.Find(studentId);
if (student != null)
{
// Access student properties (e.g., student.FirstName)
}
else
{
// Student not found
}
}

Update a Student:

using (var context = new SchoolDbContext())
{
var student = context.Students.Find(studentId);
if (student != null)
{
student.FirstName = "UpdatedFirstName";
context.SaveChanges();
}
}

Delete a Student:

using (var context = new SchoolDbContext())
{
var student = context.Students.Find(studentId);
if (student != null)
{
context.Students.Remove(student);
context.SaveChanges();
}
}

Conclusion: In this example, the Student class acts as an Active Record and encapsulates the database operations for the “Students” table. We can work with instances of the Student class as if they were regular C# objects, simplifying data access and promoting an object-oriented approach to database interaction.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri