|
7 місяців тому | |
---|---|---|
.. | ||
TranslateService | 10 місяців тому | |
DataProvider.cs | 8 місяців тому | |
DbConfig.cs | 8 місяців тому | |
DbContext_Extensions_UseMySql.cs | 9 місяців тому | |
README.md | 8 місяців тому | |
SqlTransactionScope.cs | 10 місяців тому | |
SqlTransactionScope_Command.cs | 10 місяців тому | |
SqlTranslateService.cs | 7 місяців тому | |
Vitorm.MySql.csproj | 7 місяців тому |
Vitorm.MySql is a lightweight yet comprehensive ORM for MySql that strikes the perfect balance between simplicity and functionality.
Similar to Dapper in its lightweight design, Vitorm offers an easy-to-use, efficient interface for data access.
However, it doesn't stop there; Vitorm goes beyond basic ORM capabilities to provide a rich feature set akin to Entity Framework.
This means you get the best of both worlds: the performance and simplicity of Dapper with the robust features and flexibility of Entity Framework, making Vitorm an ideal choice for developers seeking a powerful yet streamlined ORM solution.
source address: https://github.com/Vit-Orm/Vitorm/tree/master/src/Vitorm.MySql
Build | NuGet |
---|---|
This guide will walk you through the steps to set up and use Vitorm.MySql.
supported features:
feature | method | remarks | |
---|---|---|---|
create table | TryCreateTable | ||
drop table | TryDropTable | ||
--- | --- | --- | --- |
create records | Add AddRange | ||
retrieve records | Query Get | ||
update records | Update UpdateRange ExecuteUpdate | ||
delete records | Delete DeleteRange DeleteByKey DeleteByKeys ExecuteDelete | ||
--- | --- | --- | --- |
change table | ChangeTable | change mapping table from database | |
change database | ChangeDatabase | change database to be connected | |
--- | --- | --- | --- |
collection total count | TotalCount | Collection Total Count without Take and Skip | |
collection total count and list | ToListAndTotalCount | query List and TotalCount in one request | |
Before using Vitorm, install the necessary package:
dotnet add package Vitorm.MySql
code address: Program_Min.cs
using Vitorm; namespace App { public class Program_Min { static void Main2(string[] args) { // #1 Init using var dbContext = new Vitorm.Sql.SqlDbContext(); dbContext.UseMySql("Data Source=localhost;Port=3306;Database=dev-orm;SslMode=none;User Id=root;Password=123456;CharSet=utf8;allowPublicKeyRetrieval=true;"); // #2 Query var user = dbContext.Get<User>(1); var users = dbContext.Query<User>().Where(u => u.name.Contains("li")).ToList(); } // Entity Definition [System.ComponentModel.DataAnnotations.Schema.Table("User")] public class User { [System.ComponentModel.DataAnnotations.Key] public int id { get; set; } public string name { get; set; } public DateTime? birth { get; set; } public int? fatherId { get; set; } } } }
This example provides a comprehensive guide to utilizing Vitorm for basic and advanced database operations while maintaining lightweight performance.
code address: Program.csusing Vitorm; namespace App { public class Program { static void Main(string[] args) { // #1 Configures Vitorm using var dbContext = new Vitorm.Sql.SqlDbContext(); dbContext.UseMySql("Data Source=localhost;Port=3306;Database=dev-orm;SslMode=none;User Id=root;Password=123456;CharSet=utf8;allowPublicKeyRetrieval=true;"); // #2 Create Table dbContext.TryDropTable<User>(); dbContext.TryCreateTable<User>(); // #3 Insert Records dbContext.Add(new User { id = 1, name = "lith" }); dbContext.AddRange(new[] { new User { id = 2, name = "lith", fatherId = 1 }, new User { id = 3, name = "lith", fatherId = 1 } }); // #4 Query Records { var user = dbContext.Get<User>(1); var users = dbContext.Query<User>().Where(u => u.name.Contains("li")).ToList(); var sql = dbContext.Query<User>().Where(u => u.name.Contains("li")).ToExecuteString(); } // #5 Update Records dbContext.Update(new User { id = 1, name = "lith1" }); dbContext.UpdateRange(new[] { new User { id = 2, name = "lith2", fatherId = 1 }, new User { id = 3, name = "lith3", fatherId = 2 } }); dbContext.Query<User>().Where(u => u.name.Contains("li")) .ExecuteUpdate(u => new User { name = "Lith" + u.id }); // #6 Delete Records dbContext.Delete<User>(new User { id = 1, name = "lith1" }); dbContext.DeleteRange(new[] { new User { id = 2, name = "lith2", fatherId = 1 }, new User { id = 3, name = "lith3", fatherId = 2 } }); dbContext.DeleteByKey<User>(1); dbContext.DeleteByKeys<User, int>(new[] { 1, 2 }); dbContext.Query<User>().Where(u => u.name.Contains("li")) .ExecuteDelete(); dbContext.Truncate<User>(); // #7 Join Queries { var query = from user in dbContext.Query<User>() from father in dbContext.Query<User>().Where(father => user.fatherId == father.id).DefaultIfEmpty() where father != null orderby user.id select new { user, father }; var sql = query.ToExecuteString(); var users = query.ToList(); } // #8 Transactions { using var tran1 = dbContext.BeginTransaction(); dbContext.Update(new User { id = 4, name = "u4001" }); using (var tran2 = dbContext.BeginTransaction()) { dbContext.Update(new User { id = 4, name = "u4002" }); // will rollback } using (var tran2 = dbContext.BeginTransaction()) { dbContext.Update(new User { id = 4, name = "u4002" }); tran2.Rollback(); } using (var tran2 = dbContext.BeginTransaction()) { dbContext.Update(new User { id = 4, name = "u4003" }); tran2.Commit(); } tran1.Commit(); } } // Entity Definition [System.ComponentModel.DataAnnotations.Schema.Table("User")] public class User { [System.ComponentModel.DataAnnotations.Key] public int id { get; set; } public string name { get; set; } public DateTime? birth { get; set; } public int? fatherId { get; set; } } } }
User
table.Vitorm.Data is a static class that allows you to use Vitorm without explicitly creating or disposing of a DbContext.
Before using Vitorm.Data, install the necessary package:
dotnet add package Vitorm.Data
dotnet add package Vitorm.MySql
// appsettings.json
{
"Vitorm": {
"Data": [
{
"provider": "MySql",
"namespace": "App",
"connectionString": "Data Source=localhost;Port=3306;Database=dev-orm;SslMode=none;User Id=root;Password=123456;CharSet=utf8;allowPublicKeyRetrieval=true;"
}
]
}
}
After configuring the
appsettings.json
file, you can directly perform queries without any additional configuration or initialization,Vitorm.Data
is that easy to use.
code address: Program_Min.csusing Vitorm; namespace App { public class Program_Min { static void Main2(string[] args) { // Query Records var user = Data.Get<User>(1); var users = Data.Query<User>().Where(u => u.name.Contains("li")).ToList(); } // Entity Definition [System.ComponentModel.DataAnnotations.Schema.Table("User")] public class User { [System.ComponentModel.DataAnnotations.Key] public int id { get; set; } public string name { get; set; } public DateTime? birth { get; set; } public int? fatherId { get; set; } } } }
code address: Program.cs
using Vitorm; namespace App { public class Program { static void Main(string[] args) { // #1 No need to init Vitorm.Data // #2 Create Table Data.TryDropTable<User>(); Data.TryCreateTable<User>(); // #3 Insert Records Data.Add(new User { id = 1, name = "lith" }); Data.AddRange(new[] { new User { id = 2, name = "lith", fatherId = 1 }, new User { id = 3, name = "lith", fatherId = 1 } }); // #4 Query Records { var user = Data.Get<User>(1); var users = Data.Query<User>().Where(u => u.name.Contains("li")).ToList(); var sql = Data.Query<User>().Where(u => u.name.Contains("li")).ToExecuteString(); } // #5 Update Records Data.Update(new User { id = 1, name = "lith1" }); Data.UpdateRange(new[] { new User { id = 2, name = "lith2", fatherId = 1 }, new User { id = 3, name = "lith3", fatherId = 2 } }); Data.Query<User>().Where(u => u.name.Contains("li")) .ExecuteUpdate(u => new User { name = "Lith" + u.id }); // #6 Delete Records Data.Delete<User>(new User { id = 1, name = "lith1" }); Data.DeleteRange(new[] { new User { id = 2, name = "lith2", fatherId = 1 }, new User { id = 3, name = "lith3", fatherId = 2 } }); Data.DeleteByKey<User>(1); Data.DeleteByKeys<User, int>(new[] { 1, 2 }); Data.Query<User>().Where(u => u.name.Contains("li")) .ExecuteDelete(); Data.Truncate<User>(); // #7 Join Queries { var query = from user in Data.Query<User>() from father in Data.Query<User>().Where(father => user.fatherId == father.id).DefaultIfEmpty() where father != null orderby user.id select new { user, father }; var sql = query.ToExecuteString(); var users = query.ToList(); } // #8 Transactions { using var dbContext = Data.DataProvider<User>().CreateSqlDbContext(); using var tran1 = dbContext.BeginTransaction(); dbContext.Update(new User { id = 4, name = "u4001" }); using (var tran2 = dbContext.BeginTransaction()) { dbContext.Update(new User { id = 4, name = "u4002" }); // will rollback } using (var tran2 = dbContext.BeginTransaction()) { dbContext.Update(new User { id = 4, name = "u4002" }); tran2.Rollback(); } using (var tran2 = dbContext.BeginTransaction()) { dbContext.Update(new User { id = 4, name = "u4003" }); tran2.Commit(); } tran1.Commit(); } } // Entity Definition [System.ComponentModel.DataAnnotations.Schema.Table("User")] public class User { [System.ComponentModel.DataAnnotations.Key] public int id { get; set; } public string name { get; set; } public DateTime? birth { get; set; } public int? fatherId { get; set; } } } }