# Vitorm.Data Vitorm is a lightweight yet comprehensive ORM 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. > written by ChatGPT : "Help me write a description for Vitorm. It is a lightweight ORM, similar to Dapper, but it is feature-rich, akin to EF. Please emphasize its lightweight nature and comprehensive functionality." > source address: [https://github.com/VitormLib/Vitorm](https://github.com/VitormLib/Vitorm "https://github.com/VitormLib/Vitorm/tree/master/src/Vitorm.Data") ![](https://img.shields.io/github/license/VitormLib/Vitorm.svg) ![](https://img.shields.io/github/repo-size/VitormLib/Vitorm.svg) ![](https://img.shields.io/github/last-commit/VitormLib/Vitorm.svg) | Build | NuGet | | -------- | -------- | |![](https://github.com/VitormLib/Vitorm/workflows/ki_devops3/badge.svg) | [![](https://img.shields.io/nuget/v/Vitorm.Data.svg)](https://www.nuget.org/packages/Vitorm.Data) ![](https://img.shields.io/nuget/dt/Vitorm.Data.svg) | # Vitorm.Data Documentation Vitorm.Data is a static class that allows you to use Vitorm without explicitly creating or disposing of a DbContext. ## Installation Before using Vitorm.Data, install the necessary package: ``` bash dotnet add package Vitorm.Data dotnet add package Vitorm.Sqlite ``` ## Config settings ``` json // appsettings.json { "Vitorm": { "Data": [ { "provider": "Sqlite", "namespace": "App", "connectionString": "data source=sqlite.db;" } ] } } ``` ## Using Vitorm.Data ``` csharp using Vit.Extensions.Vitorm_Extensions; using Vitorm; namespace App { public class Program { static void Main(string[] args) { // #1 No need to init Vitorm.Data // #2 Create Table Data.Drop(); Data.Create(); // #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(1); var users = Data.Query().Where(u => u.name.Contains("li")).ToList(); var sql = Data.Query().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().Where(u => u.name.Contains("li")) .ExecuteUpdate(u => new User { name = "Lith" + u.id }); // #6 Delete Records Data.Delete(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(1); Data.DeleteByKeys(new[] { 1, 2 }); Data.Query().Where(u => u.name.Contains("li")) .ExecuteDelete(); // #7 Join Queries { var query = from user in Data.Query() from father in Data.Query().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().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(); } // #9 Database Functions { // select * from User where IIF(t0.fatherId is not null, true, false); var query = Data.Query().Where(u => DbFunction.Call("IIF", u.fatherId != null, true, false)); var sql = query.ToExecuteString(); var userList = query.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; } } } } ``` [Console Example](https://github.com/VitormLib/Vitorm/tree/master/test/Vitorm.Data.Console) [Test Example](https://github.com/VitormLib/Vitorm/tree/master/test/Vitorm.Data.MsTest)