@@ -926,11 +926,17 @@ Creating tables is a simple 1-liner:
926926``` csharp
927927 using (IDbConnection db = " :memory:" .OpenDbConnection ())
928928 {
929- const bool overwrite = false ;
930- db .CreateTables ( overwrite , typeof ( Shipper ), typeof ( ShipperType ) );
929+ db . CreateTable < ShipperType >() ;
930+ db .CreateTable < Shipper >( );
931931 }
932932
933933 /* In debug mode the line above prints:
934+ DEBUG: CREATE TABLE "ShipperTypes"
935+ (
936+ "ShipperTypeID" INTEGER PRIMARY KEY AUTOINCREMENT,
937+ "Name" VARCHAR(40) NOT NULL
938+ );
939+ DEBUG: CREATE UNIQUE INDEX uidx_shippertypes_name ON "ShipperTypes" ("Name" ASC);
934940 DEBUG: CREATE TABLE "Shippers"
935941 (
936942 "ShipperID" INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -941,37 +947,30 @@ Creating tables is a simple 1-liner:
941947 CONSTRAINT "FK_Shippers_ShipperTypes" FOREIGN KEY ("ShipperTypeId") REFERENCES "ShipperTypes" ("ShipperID")
942948 );
943949 DEBUG: CREATE UNIQUE INDEX uidx_shippers_companyname ON "Shippers" ("CompanyName" ASC);
944- DEBUG: CREATE TABLE "ShipperTypes"
945- (
946- "ShipperTypeID" INTEGER PRIMARY KEY AUTOINCREMENT,
947- "Name" VARCHAR(40) NOT NULL
948- );
949- DEBUG: CREATE UNIQUE INDEX uidx_shippertypes_name ON "ShipperTypes" ("Name" ASC);
950950 */
951951```
952952
953953### Transaction Support
954954As we have direct access to IDbCommand and friends - playing with transactions is easy:
955955
956956``` csharp
957- int trainsTypeId , planesTypeId ;
958- using (IDbTransaction dbTrans = db .OpenTransaction ())
959- {
960- db .Insert (new ShipperType { Name = " Trains" });
961- trainsTypeId = (int ) db .GetLastInsertId ();
957+ var trainsType = new ShipperType { Name = " Trains" };
958+ var planesType = new ShipperType { Name = " Planes" };
962959
963- db .Insert (new ShipperType { Name = " Planes" });
964- planesTypeId = (int ) db .GetLastInsertId ();
960+ // Playing with transactions
961+ using (IDbTransaction dbTrans = db .OpenTransaction ())
962+ {
963+ db .Save (trainsType );
964+ db .Save (planesType );
965965
966- dbTrans .Commit ();
967- }
968- using (IDbTransaction dbTrans = db .OpenTransaction (IsolationLevel .ReadCommitted ))
969- {
970- db .Insert (new ShipperType { Name = " Automobiles" });
971- Assert .That (db .Select <ShipperType >(), Has .Count (3 ));
966+ dbTrans .Commit ();
967+ }
972968
973- dbTrans .Rollback ();
974- }
969+ using (IDbTransaction dbTrans = db .OpenTransaction (IsolationLevel .ReadCommitted ))
970+ {
971+ db .Insert (new ShipperType { Name = " Automobiles" });
972+ Assert .That (db .Select <ShipperType >(), Has .Count .EqualTo (3 ));
973+ }
975974 Assert .That (db .Select <ShipperType >(), Has .Count (2 ));
976975```
977976
@@ -980,53 +979,52 @@ No ORM is complete without the standard crud operations:
980979
981980``` csharp
982981 // Performing standard Insert's and Selects
983- db .Insert (new Shipper { CompanyName = " Trains R Us" , Phone = " 555-TRAINS" , ShipperTypeId = trainsTypeId });
984- db .Insert (new Shipper { CompanyName = " Planes R Us" , Phone = " 555-PLANES" , ShipperTypeId = planesTypeId });
985- db .Insert (new Shipper { CompanyName = " We do everything!" , Phone = " 555-UNICORNS" , ShipperTypeId = planesTypeId });
986-
987- var trainsAreUs = db .First <Shipper >(" ShipperTypeId = {0}" , trainsTypeId );
988- Assert .That (trainsAreUs .CompanyName , Is .EqualTo (" Trains R Us" ));
989- Assert .That (db .Select <Shipper >(" CompanyName = {0} OR Phone = {1}" , " Trains R Us" , " 555-UNICORNS" ), Has .Count (2 ));
990- Assert .That (db .Select <Shipper >(" ShipperTypeId = {0}" , planesTypeId ), Has .Count (2 ));
991-
992- // Lets update a record
993- trainsAreUs .Phone = " 666-TRAINS" ;
994- db .Update (trainsAreUs );
995- Assert .That (db .SingleById <Shipper >(trainsAreUs .Id ).Phone , Is .EqualTo (" 666-TRAINS" ));
996-
997- // Then make it disappear
998- db .Delete (trainsAreUs );
999- Assert .That (db .SingleById <Shipper >(trainsAreUs .Id ), Is .Null );
1000-
1001- // And bring it back again
1002- db .Insert (trainsAreUs );
982+ db .Insert (new Shipper { CompanyName = " Trains R Us" , Phone = " 555-TRAINS" , ShipperTypeId = trainsType . Id });
983+ db .Insert (new Shipper { CompanyName = " Planes R Us" , Phone = " 555-PLANES" , ShipperTypeId = planesType . Id });
984+ db .Insert (new Shipper { CompanyName = " We do everything!" , Phone = " 555-UNICORNS" , ShipperTypeId = planesType . Id });
985+
986+ var trainsAreUs = db .SingleFmt <Shipper >(" ShipperTypeId = {0}" , trainsType . Id );
987+ Assert .That (trainsAreUs .CompanyName , Is .EqualTo (" Trains R Us" ));
988+ Assert .That (db .SelectFmt <Shipper >(" CompanyName = {0} OR Phone = {1}" , " Trains R Us" , " 555-UNICORNS" ), Has .Count . EqualTo (2 ));
989+ Assert .That (db .SelectFmt <Shipper >(" ShipperTypeId = {0}" , planesType . Id ), Has .Count . EqualTo (2 ));
990+
991+ // Lets update a record
992+ trainsAreUs .Phone = " 666-TRAINS" ;
993+ db .Update (trainsAreUs );
994+ Assert .That (db .SingleById <Shipper >(trainsAreUs .Id ).Phone , Is .EqualTo (" 666-TRAINS" ));
995+
996+ // Then make it dissappear
997+ db .Delete (trainsAreUs );
998+ Assert .That (db .SingleById <Shipper >(trainsAreUs .Id ), Is .Null );
999+
1000+ // And bring it back again
1001+ db .Insert (trainsAreUs );
10031002```
10041003
10051004### Performing custom queries
10061005And with access to raw sql when you need it - the database is your oyster :)
10071006
10081007``` csharp
1009- // Select only a subset from the table
1010- var partialColumns = db .Select <SubsetOfShipper >(typeof (Shipper ), " ShipperTypeId = {0}" , planesTypeId );
1011- Assert .That (partialColumns , Has .Count (2 ));
1008+ var partialColumns = db .SelectFmt <SubsetOfShipper >(typeof (Shipper ), " ShipperTypeId = {0}" , planesType .Id );
1009+ Assert .That (partialColumns , Has .Count .EqualTo (2 ));
10121010
1013- // Select into another POCO class that matches the sql results
1014- var rows = db .Select <ShipperTypeCount >(
1015- " SELECT ShipperTypeId, COUNT(*) AS Total FROM Shippers GROUP BY ShipperTypeId ORDER BY COUNT(*)" );
1011+ // Select into another POCO class that matches sql
1012+ var rows = db .SelectFmt <ShipperTypeCount >(
1013+ " SELECT ShipperTypeId, COUNT(*) AS Total FROM Shippers GROUP BY ShipperTypeId ORDER BY COUNT(*)" );
10161014
1017- Assert .That (rows , Has .Count (2 ));
1018- Assert .That (rows [0 ].ShipperTypeId , Is .EqualTo (trainsTypeId ));
1019- Assert .That (rows [0 ].Total , Is .EqualTo (1 ));
1020- Assert .That (rows [1 ].ShipperTypeId , Is .EqualTo (planesTypeId ));
1021- Assert .That (rows [1 ].Total , Is .EqualTo (2 ));
1015+ Assert .That (rows , Has .Count . EqualTo (2 ));
1016+ Assert .That (rows [0 ].ShipperTypeId , Is .EqualTo (trainsType . Id ));
1017+ Assert .That (rows [0 ].Total , Is .EqualTo (1 ));
1018+ Assert .That (rows [1 ].ShipperTypeId , Is .EqualTo (planesType . Id ));
1019+ Assert .That (rows [1 ].Total , Is .EqualTo (2 ));
10221020
10231021
1024- // And finally lets quickly clean up the mess we've made:
1025- db .DeleteAll <Shipper >();
1026- db .DeleteAll <ShipperType >();
1022+ // And finally lets quickly clean up the mess we've made:
1023+ db .DeleteAll <Shipper >();
1024+ db .DeleteAll <ShipperType >();
10271025
1028- Assert .That (db .Select <Shipper >(), Has .Count (0 ));
1029- Assert .That (db .Select <ShipperType >(), Has .Count (0 ));
1026+ Assert .That (db .Select <Shipper >(), Has .Count . EqualTo (0 ));
1027+ Assert .That (db .Select <ShipperType >(), Has .Count . EqualTo (0 ));
10301028```
10311029
10321030## Other notable Micro ORMs for .NET
0 commit comments