MSDN Documentation

Core Data Access in .NET

This tutorial provides a comprehensive guide to accessing data within your .NET Core applications. We'll explore various strategies, from traditional ADO.NET to modern ORM solutions like Entity Framework Core.

Understanding Data Access Layers

A well-structured data access layer (DAL) is crucial for maintaining clean, maintainable, and testable code. It abstracts the complexities of data storage and retrieval, allowing your application's business logic to focus on its core responsibilities.

Key principles of a good DAL include:

ADO.NET Fundamentals

ADO.NET is the foundational data access technology in .NET. It provides a set of classes for connecting to data sources, executing commands, and retrieving results. While powerful, it can be verbose for common tasks.

Key ADO.NET objects:

Example: Reading Data with ADO.NET


using System;
using System.Data;
using Microsoft.Data.SqlClient; // Or your specific provider

public class CustomerRepository
{
    private readonly string _connectionString;

    public CustomerRepository(string connectionString)
    {
        _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
    }

    public string GetCustomerName(int customerId)
    {
        using (var connection = new SqlConnection(_connectionString))
        {
            var command = new SqlCommand("SELECT Name FROM Customers WHERE CustomerID = @CustomerID", connection);
            command.Parameters.AddWithValue("@CustomerID", customerId);

            connection.Open();
            var reader = command.ExecuteReader();

            if (reader.Read())
            {
                return reader.GetString(0);
            }
            return null;
        }
    }
}
            

Entity Framework Core (EF Core)

Entity Framework Core is Microsoft's modern, cross-platform Object-Relational Mapper (ORM). It simplifies data access by allowing you to work with data as .NET objects, rather than writing raw SQL queries. EF Core supports various database providers for SQL Server, PostgreSQL, MySQL, SQLite, and more.

Key EF Core Concepts:

Example: Using EF Core

First, define your entity and DbContext:


public class Customer
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

public class MyDbContext : DbContext
{
    public DbSet Customers { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Replace with your actual connection string and provider
        optionsBuilder.UseSqlServer("YourConnectionStringHere");
    }
}
            

Then, use it in your application:


using (var context = new MyDbContext())
{
    var customer = context.Customers.FirstOrDefault(c => c.CustomerId == 1);
    if (customer != null)
    {
        Console.WriteLine($"Customer Name: {customer.Name}");
    }
}
            

Choosing the Right Approach

The choice between ADO.NET and EF Core (or other ORMs) depends on your project's needs:

Best Practices

Continue to the next section for exploring web development concepts in .NET Core.