Getting Started
This article provides a step-by-step introduction to configure OrmDb setup, and also to build and run a simple console application using Visual Studio. Creating simple console application
  1. Start Visual Studio and create new console application named MyApp



  2. Now, install OrmDb NuGet package to the new application using the NuGet Package Manager.

    Type Ormdb in search and press Enter, then install.

    Once the installation process is completed, the OrmDb package will be installed in the project.
  3. Insert the line below for the AspNet.Core development. This script provides for managing the connection pool for the client(s).
                    <script type="text/javascript" src="https://ormdb.github.io/ormdb/ormdb.min.js" crossorigin="anonymous" async></script>
                
Start Coding
  1. Before you start, install the .Net connector for the database which you want to use. Sqlite database is used in this article.
  2. Create your connection.
                    var cn = new System.Data.SQLite.SQLiteConnection("Data Source =.\\w3schools.sqlite; Version = 3;");
                
  3. Test your connection.
                    using(var exc = cn.Executer()) {
                        Console.WriteLine("OrmDb v." + Odb.Version.ToString());
                        Console.WriteLine(String.Empty);
    
                        cn.OpenIf();
                        Console.WriteLine("Connection State = " + cn.State.ToString());
                        Console.WriteLine(String.Empty);
    
                        var cp = cn.Provider();
                        Console.WriteLine("Provider Name = " + cp.FullName);
                        Console.WriteLine("Server DateTime = " + cp.GetServerDateTime());
    
                        var si = cp.ServerInfo();
                        Console.WriteLine("Server Name = " + si.Name);
                        Console.WriteLine("Server Version = " + si.Version);
                    }
                
    OrmDb v.1.6.3.6

    Provider = Odb.Providers.Sqlite
    Connector = System.Data.SqlClient.SqlConnection

    Server DateTime = 10.12.2021 15:10:48
    Ping = 2.2347ms

    Server Name = sqlite
    Server Version = 3.37.0
    jJ0DZ7
Start to CRUD Operations
  1. Defining Models and Tables. The Models can be used also as table definition directly.
                    using Odb;
                    using Odb.Table;
                    using System.Linq.Expressions;
    
                    namespace MyApp.Models {
                        public class Orders : Base
                        {
                            [Column()] public int? OrderID { get; set; }
                            [Column()] public int? CustomerID { get; set; }
                            [Column()] public int? EmployeeID { get; set; }
                            [Column()] public DateTime OrderDate { get; set; }
                            [Column(DefaultValue = 1)] public int? ShipperID { get; set; }
                            [Column(Length = 255)] public string Note { get; set; }
    
                            public List<OrderDetails> Details { get; set; }
    
                            protected override Expression<TableConfig> Config() =>
                                (x) => x.SetPrimaryKey(OrderID);
                        }
    
    
                        public class OrderDetails : Base
                        {
                            [Column(AutoIncrement = true)] public int OrderDetailID { get; set; }
                            [Column()] public int? OrderID { get; set; }
                            [Column()] public int? ProductID { get; set; }
                            [Column()] public decimal Quantity { get; set; }
    
                            protected override Expression<TableConfig> Config() =>
                                 (x) => x.SetPrimaryKey(OrderDetailID);
                        }
                    }
                
  2. Create tables on database. The tables should be created before CRUD operations. If you want, tables can be created programmatically by Ormdb. You can skip this section if the tables are already exists in database.
                    using(var exc = cn.Executer()) {
    
                        var tables = exc.GetTables();
                        foreach(var tb in new Odb.Table.Base[] {Models.Orders , Models.OrderDetails}){
                            if (tables.Exists(x => x.Name.ToLower() == tb.GetName().ToLower()))
                                continue;
    
                            exc.CreateTable(tb);
                        }
    
                    }
                
  3. Insertion the records.
                    // ---------------------------------------------
                    // sample data ,s being generated
                    var Sample_Orders = new List<Models.Orders>();
    
                    var id = 0;
                    var order_date = new DateTime(2000, 06, 1);
                    foreach (var customer_id in new[] { 11, 12, 13, 14, 15 })
                    {
                        foreach (var employee_id in new[] { 21, 22, 23 })
                        {
                            foreach (var shipper_id in new[] { 31, 32 })
                            {
                                id++;
    
                                var item = new Models.Orders();
                                item.OrderID = 1000 + id;
                                item.CustomerID = customer_id;
                                item.EmployeeID = employee_id;
                                item.ShipperID = shipper_id;
                                item.OrderDate = order_date;
                                item.Note = "Order Number = " + item.OrderID;
    
                                item.Details = new List<Models.OrderDetails>();
                                item.Details.Add(new OrderDetails() { OrderID = item.OrderID, ProductID = 41, Quantity = 10 });
                                item.Details.Add(new OrderDetails() { OrderID = item.OrderID, ProductID = 42, Quantity = 15 });
                                item.Details.Add(new OrderDetails() { OrderID = item.OrderID, ProductID = 43, Quantity = 5 });
                                item.Details.Add(new OrderDetails() { OrderID = item.OrderID, ProductID = 44, Quantity = 30 });
    
                                Sample_Orders.Add(item);
                            }
                        }
                        order_date = order_date.AddDays(1);
                    }
    
                    // ---------------------------------------------
    
                    using(var exc = cn.Executer()) {
    
                        var query = Odb.SQL.Insert();
                        foreach (var order in Sample_Orders)
                        {
                            query.Add(ord);
                            foreach (var detail in order.Details)
                                query.Add(det);
                        }
    
                        var affected_rows = exc.Sql(query).NonQuery();
                    }
                
  4. // Table Aliases
                    
                    var t1 = new Models.Orders().AsQueryable();
    
                    // If the model will be use twice in a query, then you can define multiple alias for the same model.
                    var t2 = new Models.Orders().AsQueryable();
                
  5. Selecting the records.
                    using(var exc = cn.Executer()) {
    
                        var query = Odb.SQL.Select(x => x.From(t1));
                        var compiledQuery = exc.Sql(query);
    
                        var dt = compiledQuery.Read().ToDataTable();
                        var orders = compiledQuery.Map<Orders>();
    
                    }
                
  6. Updating the records.
                    using(var exc = cn.Executer()) {
    
                        var query = Odb.SQL.Update(x =>
                                          x.From(t1)
                                           .Where(t1.OrderID == 1000)
                                           .Set(t1.OrderID.OrderDate, DateTime.Now)
                                    );
    
                        var affected_rows = exc.Sql(query).NonQuery();
    
                    }
                
  7. Deleting the records.
                    using(var exc = cn.Executer()) {
    
                        var query = Odb.SQL.Delete(x =>
                                          x.From(t1)
                                           .Where(t1.OrderID == 1000)
                                    );
    
                        var affected_rows = exc.Sql(query).NonQuery();
    
                    }