Entity Framework Core (EF Core) is a modern, open-source, and cross-platform Object-Relational Mapper (ORM) for .NET. It allows developers to work with a database using .NET objects, eliminating most of the data-access code that developers usually need to write. EF Core supports LINQ queries, change tracking, updates, and schema migrations.
Productivity: EF Core simplifies data access by reducing the amount of boilerplate code required for database operations.
LINQ Support: It allows you to write database queries using LINQ, which is type-safe and easier to understand.
Cross-platform: EF Core works on multiple platforms, including Windows, Linux, and macOS.
Migrations: It helps manage database schema changes through a migration API.
Install EF Core: You can install EF Core via NuGet Package Manager or the Package Manager Console:
Summary: The constructor initializes the class with a BlogContext object, which is used to manage database interactions.
Read Method
Summary: This method retrieves all blog entries from the Blogs DbSet, converts the results to a list of BlogModel objects, and prints each blog's data to the console.
Edit Method
Summary: This method retrieves a specific blog entry based on the provided ID, prints the details if found, or indicates that no data was found.
Create Method
Summary: This method creates a new blog entry, adds it to the Blogs DbSet, and saves the changes to the database.
Update Method
Summary: This method updates an existing blog entry based on the provided ID, title, author, and content, and saves the changes to the database.
Delete Method
Summary: This method deletes a blog entry from the Blogs DbSet based on the provided ID, and saves the changes to the database.
Helper Method: Convert DataTable to List of BlogModel
In EF Core, you don't need a helper method to convert a DataTable to a list of models because EF Core handles this conversion internally.
Summary: By using EF Core, you can simplify database operations while maintaining strong typing and relationships between your data models. EF Core's support for LINQ queries, change tracking, updates, and migrations makes it a powerful tool for managing database interactions in .NET applications.
using Microsoft.EntityFrameworkCore;
public class BlogContext : DbContext
{
public DbSet<BlogModel> Blogs { get; set; }
public BlogContext(DbContextOptions<BlogContext> options)
: base(options)
{
}
}
public class EfCoreExample
{
private readonly BlogContext _context;
public EfCoreExample(BlogContext context)
{
_context = context;
}
}
public void Read()
{
var blogs = _context.Blogs.ToList();
foreach (var blog in blogs)
{
Console.WriteLine($"Blog Id => {blog.BlogId}");
Console.WriteLine($"Blog Title => {blog.BlogTitle}");
Console.WriteLine($"Blog Author => {blog.BlogAuthor}");
Console.WriteLine($"Blog Content => {blog.BlogContent}");
Console.WriteLine("--------------------------------");
}
}
public void Edit(int id)
{
var blog = _context.Blogs.Find(id);
if (blog == null)
{
Console.WriteLine("No data found.");
return;
}
Console.WriteLine($"Blog Id => {blog.BlogId}");
Console.WriteLine($"Blog Title => {blog.BlogTitle}");
Console.WriteLine($"Blog Author => {blog.BlogAuthor}");
Console.WriteLine($"Blog Content => {blog.BlogContent}");
}
public void Create(string title, string author, string content)
{
var blog = new BlogModel
{
BlogTitle = title,
BlogAuthor = author,
BlogContent = content
};
_context.Blogs.Add(blog);
_context.SaveChanges();
Console.WriteLine("Saving Successful.");
}
public void Update(int id, string title, string author, string content)
{
var blog = _context.Blogs.Find(id);
if (blog == null)
{
Console.WriteLine("No data found.");
return;
}
blog.BlogTitle = title;
blog.BlogAuthor = author;
blog.BlogContent = content;
_context.SaveChanges();
Console.WriteLine("Updating Successful.");
}
public void Delete(int id)
{
var blog = _context.Blogs.Find(id);
if (blog == null)
{
Console.WriteLine("No data found.");
return;
}
_context.Blogs.Remove(blog);
_context.SaveChanges();
Console.WriteLine("Deleting Successful.");
}