Skip to content
Sann Lynn Htun edited this page Nov 21, 2024 · 2 revisions

EFCore

Entity Framework Core (EFCore) is an ORM for .NET, helping to manage database interactions.

1. AppDbContext Class

public class AppDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(ConnectionStrings.SqlConnectionStringBuilder.ConnectionString);
    }

    public DbSet<BlogDto> Blogs { get; set; }
}

Summary: This class defines the Entity Framework Core database context for your application. It configures the connection to the SQL Server database and includes a DbSet for the BlogDto entities.

2. EFCoreExample Constructor

public EFCoreExample(AppDbContext db)
{
    _db = db;
}

Summary: The constructor initializes the class with an AppDbContext object, which is used to interact with the database.

3. Run Method

public void Run()
{
    Read();
    Create("title", "author", "content");
    Edit(1);
    Update(1, "title 2", "author 2", "content 2");
    Delete(1);
}

Summary: This method serves as an entry point for testing different operations. It calls the CRUD methods (Create, Read, Update, Delete).

4. Read Method

private void Read()
{
    var blogs = _db.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("----------------------------");
    }
}

Summary: This method retrieves all rows from the Blogs table and prints each row's data to the console.

5. Edit Method

private void Edit(int id)
{
    var blog = _db.Blogs.FirstOrDefault(b => b.BlogId == id);
    if (blog is 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}");
}

Summary: This method retrieves a specific blog entry based on the provided id and prints the details if found. If no entry is found, it indicates that no data was found.

6. Create Method

private void Create(string title, string author, string content)
{
    var blog = new BlogDto
    {
        BlogTitle = title,
        BlogAuthor = author,
        BlogContent = content
    };

    _db.Blogs.Add(blog);
    int result = _db.SaveChanges();

    string message = result > 0 ? "Saving Successful." : "Saving Failed.";
    Console.WriteLine(message);
}

Summary: This method inserts a new blog entry into the Blogs table with the provided title, author, and content. It indicates whether the save operation was successful.

7. Update Method

private void Update(int id, string title, string author, string content)
{
    var blog = _db.Blogs.FirstOrDefault(b => b.BlogId == id);
    if (blog is null)
    {
        Console.WriteLine("No data found.");
        return;
    }

    blog.BlogTitle = title;
    blog.BlogAuthor = author;
    blog.BlogContent = content;

    int result = _db.SaveChanges();

    string message = result > 0 ? "Updating Successful." : "Updating Failed.";
    Console.WriteLine(message);
}

Summary: This method updates an existing blog entry in the Blogs table based on the provided id, title, author, and content. It indicates whether the update operation was successful.

8. Delete Method

private void Delete(int id)
{
    var blog = _db.Blogs.FirstOrDefault(b => b.BlogId == id);
    if (blog is null)
    {
        Console.WriteLine("No data found.");
        return;
    }

    _db.Blogs.Remove(blog);
    int result = _db.SaveChanges();

    string message = result > 0 ? "Deleting Successful." : "Deleting Failed.";
    Console.WriteLine(message);
}

Summary: This method deletes a blog entry from the Blogs table based on the provided id. It indicates whether the delete operation was successful.

C# Basics Wiki

Core Concepts

Object-Oriented Programming (OOP)

Advanced Topics

Miscellaneous

Tools and Resources

Clone this wiki locally