Entity Framework Core is a lightweight, extensible, open-source version of the popular Entity Framework data access technology. It is designed to work with .NET applications, enabling developers to interact with a database using strongly typed C# objects instead of writing raw SQL queries. This approach simplifies data access layers and promotes cleaner code.
Start by creating a new .NET Core application. You can do this using the .NET CLI (Command Line Interface):
dotnet new console -n MyEFCoreApp
Navigate into your project directory:
cd MyEFCoreApp
To use EF Core, you need to install the relevant NuGet packages. The following command will add EF Core and the SQL Server provider (if you are using SQL Server):
dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Tools
The database context acts as a bridge between your domain or entity classes and the database. Create a new class named MyDbContext.cs:
using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Product> Products { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("YourConnectionStringHere"); } }
In this example, we define a DbSet<Product>, which represents the collection of products in the database. Make sure to replace "YourConnectionStringHere" with your actual database connection string.
Next, create a class that represents the data you’ll be working with. For instance, let's create a simple Product class:
public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } }
This Product class represents the items we'll store in our database, with an Id, name, and price.
You can use EF Core Migrations to create the database schema. To do this, first enable migrations:
dotnet ef migrations add InitialCreate
Then, apply the migration to create the database:
dotnet ef database update
This command generates the database based on the model defined in your context and entity classes.
To add a new product to the database, use the following code in the Main method of your Program.cs:
using (var context = new MyDbContext()) { var product = new Product { Name = "Example Product", Price = 9.99m }; context.Products.Add(product); context.SaveChanges(); }
This code creates a new Product instance, adds it to the Products DbSet, and saves the changes to the database.
To retrieve products from the database:
using (var context = new MyDbContext()) { var products = context.Products.ToList(); foreach (var product in products) { Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Price: {product.Price}"); } }
To update an existing product:
using (var context = new MyDbContext()) { var product = context.Products.Find(1); // Find by Id if (product != null) { product.Price = 14.99m; context.SaveChanges(); } }
To remove a product:
using (var context = new MyDbContext()) { var product = context.Products.Find(1); if (product != null) { context.Products.Remove(product); context.SaveChanges(); } }
Include() to load related data as necessary, but avoid loading unnecessary data.Entity Framework Core provides a straightforward approach to data access in .NET applications, allowing you to concentrate on your application logic rather than the intricacies of SQL. With EF Core, you can efficiently perform CRUD operations, maintain a well-structured data access layer, and leverage the benefits of LINQ when querying your database.
30/10/2024 | DotNet
30/10/2024 | DotNet
30/10/2024 | DotNet
30/10/2024 | DotNet
30/10/2024 | DotNet
30/10/2024 | DotNet
30/10/2024 | DotNet