
Play Store Application link – Java to .NET in 9 Steps – App on Google Play
Welcome! If you’re transitioning from Java to C#, understanding how to work with data is crucial. In this post, we’ll cover Entity Framework Core and ADO.NET for data access in C#. We’ll use real-world examples and draw parallels to Java for a smoother learning experience.
1. Entity Framework Core
Entity Framework Core (EF Core) is an Object-Relational Mapper (ORM) for C#. It simplifies database interactions by allowing you to work with data as objects. This is similar to Java’s Hibernate.
ORM Basics
An ORM helps map database tables to C# classes. For instance, a Customer
class might map to a Customers
table in your database. EF Core automatically handles the CRUD (Create, Read, Update, Delete) operations for you.
Code-First and Database-First Approaches
- Code-First Approach: You define your classes first, and EF Core generates the database schema.
Example Code:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(“your_connection_string_here”);
}
}
To create the database, you use Entity Framework migrations:dotnet ef migrations add InitialCreate
dotnet ef database update
- Database-First Approach: You start with an existing database, and EF Core generates the classes based on the database schema.
Using Database-First:dotnet ef dbcontext scaffold "your_connection_string_here" Microsoft.EntityFrameworkCore.SqlServer
This command generates context and entity classes based on your existing database.
LINQ to Entities
LINQ (Language Integrated Query) is used to query data from the database in a readable format. It’s similar to using Streams in Java.
Example Code:
using (var context = new AppDbContext())
{
var customers = from c in context.Customers
where c.Name.Contains("Alice")
select c;
foreach (var customer in customers)
{
Console.WriteLine($"{customer.Id}: {customer.Name}");
}
}
2. Data Access with ADO.NET
ADO.NET provides a more direct way to interact with the database. It gives you control over database operations, similar to Java’s JDBC.
Connecting to Databases
You establish a connection to the database using SqlConnection
in C#.
Example Code:
using System.Data.SqlClient;
string connectionString = "your_connection_string_here";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("Connection Opened");
}
Executing Queries
You can execute SQL queries and commands directly using ADO.NET.
Example Code:
using System.Data.SqlClient;
string query = "SELECT * FROM Customers";
using (SqlConnection connection = new SqlConnection("your_connection_string_here"))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"{reader["Id"]}: {reader["Name"]}");
}
}
Data Readers and Data Adapters
- Data Reader: Provides a forward-only stream of data from the database. It’s similar to Java’s
ResultSet
.
Example Code:
using (SqlConnection connection = new SqlConnection(“your_connection_string_here”))
{
SqlCommand command = new SqlCommand(“SELECT * FROM Customers”, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($”{reader[“Id”]}: {reader[“Name”]}”);
}
} - Data Adapter: Fills a
DataSet
orDataTable
with data from the database. It’s similar to Java’sResultSet
but allows for more complex operations and updates.
Example Code:
using System.Data;
using System.Data.SqlClient;
string query = “SELECT * FROM Customers”;
using (SqlConnection connection = new SqlConnection(“your_connection_string_here”))
{
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataTable table = new DataTable();
adapter.Fill(table);foreach (DataRow row in table.Rows) { Console.WriteLine($"{row["Id"]}: {row["Name"]}"); }
}
This guide should help you get started with data access in C# using Entity Framework Core and ADO.NET. By leveraging your Java knowledge, you can see how these concepts translate into the C# world. Happy coding!