DbManager is a powerful package that simplifies the integration of CRUD (Create, Read, Update, Delete) operations in your .NET projects. It seamlessly supports various database systems like MS SQL Server, MySQL, and leverages the functionalities of Entity Framework Core.
Integrating DbManager into your project is as easy as 1-2-3-4:
Begin by adding DbManager to your solution as a project reference.
-
.Net CLI
dotnet add package DbManager.EFCore
-
Package Manager
Install-Package DbManager.EFCore
In your Program.cs file, register the AppDbContext class of DbManager library:
var builder = WebApplication.CreateBuilder(args);
// Use DbManager -> AppDbContext to register DbContext for your project.
builder.Services.AddDbContext<AppDbContext>();
var app = builder.Build();
In your appsettings.json file, configure the connection strings based on your database provider and entities project:
For MS SQL Server:
{
"DatabaseProvider": "SqlServer",
"EntitiesAssemblyName": "Demo.Db", // Assembly name where you have your Entity classes
"ConnectionStrings": {
"DefaultConnection": "your_sql_server_connection_string_here"
}
}
For MySQL:
{
"DatabaseProvider": "MySql",
"EntitiesAssemblyName": "Demo.Db",
"ConnectionStrings": {
"DefaultConnection": "your_mysql_connection_string_here"
}
}
[Table("Students")]
public class Student
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int RollNo { get; set; }
public string Section { get; set; }
public string BirthDate { get; set; }
public string BloodGroup { get; set; }
}
- You must mark your entity classes with [Table] annotation, otherwise "DbManager", will not be able to include them to the DbContext.
Inject the AppDbContext class into your desired Controller, Business, or Repository class. For example, in a StudentRepository class:
public class StudentRepository : BaseRepository<Student>, IStudentRepository
{
public StudentRepository(AppDbContext context) : base(context) // Injected DbManager -> AppDbContext to StudentRepository
{
}
}
-----------------------------------------------------------------------------
public interface IStudentRepository : IRepository<Student>
{
}
- In above code, BaseRepository is from "DbManager" library, and IStudentRepository must inherit from "IRepository" of "DbManager" library.
With DbManager integrated into your project, you can take advantage of all the CRUD methods already defined in the BaseRepository class. Here is a quick overview:
FindAsync
: Find a single entity based on a provided predicate.FetchListBySPAsync
: Fetch a list of entities using a stored procedure and parameters.GetAllAsync
: Get all entity objects.GetByIdAsync
: Retrieve an entity object based on its ID.InsertAsync
: Insert a new entity object into the database.UpdateAsync
: Update an existing entity object.DeleteAsync
: Delete a single entity object.
Remember, all methods are asynchronous, so ensure you use async/await properly while calling them.
Congratulations! You've successfully configured DbManager for your project!
For more detailed information and usage examples, refer to our documentation.
Note: Make sure to add a project reference to DbManager in your solution to access the BaseRepository
class and other utilities. For any queries or issues, please reach out to our support team.
Happy Coding with DbManager!