-
Notifications
You must be signed in to change notification settings - Fork 5
EFCore
Entity Framework Core (EFCore) is an ORM for .NET, helping to manage database interactions.
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.
public EFCoreExample(AppDbContext db)
{
_db = db;
}
Summary: The constructor initializes the class with an AppDbContext
object, which is used to interact with the database.
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).
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.
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.
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.
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.
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.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.