Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

340 Commits

Repository files navigation

Join the new google group or follow @demisbellot and @ServiceStack for twitter updates.

ServiceStack.OrmLite is a convention-based, configuration-free lightweight ORM that uses standard POCO classes and Data Annotation attributes to infer its table schema.

ServiceStack.OrmLite is an independent library and can be used with or without the ServiceStack webservices framework.

Introduction

OrmLite is a set of light-weight C# extension methods around System.Data.* interfaces which is designed to persist POCO classes with a minimal amount of intrusion and configuration. Another Orm with similar goals is sqlite-net by Frank Krueger.

OrmLite was designed with a focus on the core objectives:

  • Map a POCO class 1:1 to an RDBMS table, cleanly by conventions, without any attributes required.
  • Create/Drop DB Table schemas using nothing but POCO class definitions (IOTW a true code-first ORM)
  • Simplicity - typed, wrist friendly API for common data access patterns.
  • High performance - with support for indexes, text blobs, etc.
  • Expressive power and flexibility - with access to IDbCommand and raw SQL
  • Cross platform - supports multiple dbs (currently: Sql Server, Sqlite, MySql, PostgreSQL, Firebird) running on both .NET and Mono platforms.

In OrmLite: 1 Class = 1 Table. There's no hidden behaviour behind the scenes auto-magically managing hidden references to other tables. Any non-scalar properties (i.e. complex types) are text blobbed in a schema-less text field using .NET's fastest Text Serializer. Effectively this allows you to create a table from any POCO type and it should persist as expected in a DB Table with columns for each of the classes 1st level public properties.

Download

Download on NuGet

8 flavours of OrmLite is on NuGet:

OrmLite is also included in ServiceStack or available to download separately in /downloads.

New Simplified API

We've streamlined our API, now all OrmLite extensions that used to be on IDbCommand now hang off IDbConnection (just like Dapper), this reduces the boiler-plate when opening a connection to a single line, so now you can create a table and insert a record with just:

using(IDbConnectiondb=dbFactory.OpenDbConnection()){db.CreateTable<Employee>();db.Insert(newEmployee{Id=1,Name="Employee 1"});}

The methods off IDbCommand have now been deprecated and will one day be removed. Update your library.

New Foreign Key attribute for referential actions on Update/Deletes

Creating a foreign key in OrmLite can be done by adding [References(typeof(ForeignKeyTable))] on the relation property, which will result in OrmLite creating the Foreign Key relationship when it creates the DB table with db.CreateTable<Poco>. @brainless83 has extended this support further by adding more finer-grain options and behaviours with the new [ForeignKey] attribute which will now let you specify the desired behaviour when deleting or updating related rows in Foreign Key tables.

An example of a table with all the different options:

publicclassTableWithAllCascadeOptions{[AutoIncrement]publicintId{get;set;}[References(typeof(ForeignKeyTable1))]publicintSimpleForeignKey{get;set;}[ForeignKey(typeof(ForeignKeyTable2),OnDelete="CASCADE",OnUpdate="CASCADE")]publicint?CascadeOnUpdateOrDelete{get;set;}[ForeignKey(typeof(ForeignKeyTable3),OnDelete="NO ACTION")]publicint?NoActionOnCascade{get;set;}[Default(typeof(int),"17")][ForeignKey(typeof(ForeignKeyTable4),OnDelete="SET DEFAULT")]publicintSetToDefaultValueOnDelete{get;set;}[ForeignKey(typeof(ForeignKeyTable5),OnDelete="SET NULL")]publicint?SetToNullOnDelete{get;set;}}

The ForeignKeyTests show the resulting behaviour with each of these configurations in more detail.

Note: Only supported on RDBMS's with foreign key/referential action support, e.g. Sql Server, PostgreSQL, MySQL. Otherwise they're ignored.

Multi nested database connections

We now support multiple nested database connections so you can now trivially use OrmLite to access multiple databases on different connections. The OrmLiteConnectionFactory class has been extended to support named connections which allows you to conveniently define all your db connections when you register it in your IOC and access them with the named property when you use them.

A popular way of scaling RDBMS's is to create a Master / Shard setup where datasets for queries that span entire system are kept in the master database, whilst context-specific related data can be kept together in an isolated shard. This feature makes it trivial to maintain multiple separate db shards with a master database in a different RDBMS.

Here's an (entire source code) sample of the code needed to define, and populate a Master/Shard setup. Sqlite can create DB shards on the fly so only the blank SqlServer master database needed to be created out-of-band:

Sharding 1000 Robots into 10 Sqlite DB shards - referencing each in a Master SqlServer RDBMS

publicclassMasterRecord{publicGuidId{get;set;}publicintRobotId{get;set;}publicstringRobotName{get;set;}publicDateTime?LastActivated{get;set;}}publicclassRobot{publicintId{get;set;}publicstringName{get;set;}publicboolIsActivated{get;set;}publiclongCellCount{get;set;}publicDateTimeCreatedDate{get;set;}}constintNoOfShards=10;constintNoOfRobots=1000;vardbFactory=newOrmLiteConnectionFactory("Data Source=host;Initial Catalog=RobotsMaster;Integrated Security=SSPI",//Connection StringSqlServerDialect.Provider);dbFactory.Run(db =>db.CreateTable<MasterRecord>(overwrite:false));NoOfShards.Times(i =>{varnamedShard="robots-shard"+i;dbFactory.RegisterConnection(namedShard,"~/App_Data/{0}.sqlite".Fmt(shardId).MapAbsolutePath(),//Connection StringSqliteDialect.Provider);dbFactory.OpenDbConnection(namedShard).Run(db =>db.CreateTable<Robot>(overwrite:false));});varnewRobots=NoOfRobots.Times(i =>//Create 1000 RobotsnewRobot{Id=i,Name="R2D"+i,CreatedDate=DateTime.UtcNow,CellCount=DateTime.Now.ToUnixTimeMs()%100000});foreach(varnewRobotinnewRobots){using(IDbConnectiondb=dbFactory.OpenDbConnection())//Open Connection to Master DB {db.Insert(newMasterRecord{Id=Guid.NewGuid(),RobotId=newRobot.Id,RobotName=newRobot.Name});using(IDbConnectionrobotShard=dbFactory.OpenDbConnection("robots-shard"+newRobot.Id%NoOfShards))//Shard{robotShard.Insert(newRobot);}}}

Using the SQLite Manager Firefox extension we can peek at one of the created shards to see 100 Robots in each shard. This is the dump of robots-shard0.sqlite:

Data dump of Robot Shard #1

As expected each shard has every 10th robot inside.

New strong-typed Sql Expression API

We've now added SQL Expression support to bring you even nicer LINQ-liked querying to all our providers. To give you a flavour here are some examples with their partial SQL output (done in SQL Server):

Querying with SELECT

intagesAgo=DateTime.Today.AddYears(-20).Year;db.Select<Author>(q =>q.Birthday>=newDateTime(agesAgo,1,1)&&q.Birthday<=newDateTime(agesAgo,12,31));

WHERE (("Birthday" >= '1992-01-01 00:00:00.000') AND ("Birthday" <= '1992-12-31 00:00:00.000'))

db.Select<Author>(q =>Sql.In(q.City,"London","Madrid","Berlin"));

WHERE "JobCity" In ('London', 'Madrid', 'Berlin')

db.Select<Author>(q =>q.Earnings<=50);

WHERE ("Earnings" <= 50)

db.Select<Author>(q =>q.Name.StartsWith("A"));

WHERE upper("Name") like 'A%'

db.Select<Author>(q =>q.Name.EndsWith("garzon"));

WHERE upper("Name") like '%GARZON'

db.Select<Author>(q =>q.Name.Contains("Benedict"));

WHERE upper("Name") like '%BENEDICT%'

db.Select<Author>(q =>q.Rate==10&&q.City=="Mexico");

WHERE (("Rate" = 10) AND ("JobCity" = 'Mexico'))

Right now the Expression support can satisfy most simple queries with a strong-typed API. For anything more complex (e.g. queries with table joins) you can still easily fall back to raw SQL queries as seen below.

INSERT, UPDATE and DELETEs

To see the behaviour of the different APIs, all examples uses this simple model

publicclassPerson{publicintId{get;set;}publicstringFirstName{get;set;}publicstringLastName{get;set;}publicint?Age{get;set;}}

UPDATE

In its most simple form, updating any model without any filters will update every field, except the Id which is used to filter the update to this specific record:

db.Update(newPerson{Id=1,FirstName="Jimi",LastName="Hendrix",Age=27});

UPDATE "Person" SET "FirstName" = 'Jimi',"LastName" = 'Hendrix',"Age" = 27 WHERE "Id" = 1

If you supply your own where expression, it updates every field (inc. Id) but uses your filter instead:

db.Update(newPerson{Id=1,FirstName="JJ"}, p =>p.LastName=="Hendrix");

UPDATE "Person" SET "Id" = 1,"FirstName" = 'JJ',"LastName" = NULL,"Age" = NULL WHERE ("LastName" = 'Hendrix')

One way to limit the fields which gets updated is to use an Anonymous Type:

db.Update<Person>(new{FirstName="JJ"}, p =>p.LastName=="Hendrix");

Or by using UpdateNonDefaults which only updates the non-default values in your model using the filter specified:

db.UpdateNonDefaults(newPerson{FirstName="JJ"}, p =>p.LastName=="Hendrix");

UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')

UpdateOnly

As updating a partial row is a common use-case in Db's, we've added a number of methods for just this purpose, named UpdateOnly.

The first expression in an UpdateOnly statement is used to specify which fields should be updated:

db.UpdateOnly(newPerson{FirstName="JJ"}, p =>p.FirstName);

UPDATE "Person" SET "FirstName" = 'JJ'

When present, the second expression is used as the where filter:

db.UpdateOnly(newPerson{FirstName="JJ"}, p =>p.FirstName, p =>p.LastName=="Hendrix");

UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')

Instead of using the expression filters above you can choose to use an ExpressionVisitor builder which provides more flexibility when you want to programatically construct the update statement:

db.UpdateOnly(newPerson{FirstName="JJ",LastName="Hendo"}, ev =>ev.Update(p =>p.FirstName));

UPDATE "Person" SET "FirstName" = 'JJ'

db.UpdateOnly(newPerson{FirstName="JJ"}, ev =>ev.Update(p =>p.FirstName).Where(x =>x.FirstName=="Jimi"));

UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')

For the ultimate flexibility we also provide un-typed, string-based expressions. Use the .Params() extension method escape parameters (inspired by massive):

db.Update<Person>(set:"FirstName = {0}".Params("JJ"),where:"LastName = {0}".Params("Hendrix"));

Even the Table name can be a string so you perform the same update without requiring the Person model at all:

db.Update(table:"Person",set:"FirstName = {0}".Params("JJ"),where:"LastName = {0}".Params("Hendrix"));

UPDATE "Person" SET FirstName = 'JJ' WHERE LastName = 'Hendrix'

INSERT

Insert's are pretty straight forward since in most cases you want to insert every field:

db.Insert(newPerson{Id=1,FirstName="Jimi",LastName="Hendrix",Age=27});

INSERT INTO "Person" ("Id","FirstName","LastName","Age") VALUES (1,'Jimi','Hendrix',27)

But do provide an API that takes an Expression Visitor for the rare cases you don't want to insert every field

db.InsertOnly(newPerson{FirstName="Amy"}, ev =>ev.Insert(p =>new{p.FirstName}));

INSERT INTO "Person" ("FirstName") VALUES ('Amy')

DELETE

Like updates for DELETE's we also provide APIs that take a where Expression:

db.Delete<Person>(p =>p.Age==27);

Or an Expression Visitor:

db.Delete<Person>(ev =>ev.Where(p =>p.Age==27));

DELETE FROM "Person" WHERE ("Age" = 27)

As well as un-typed, string-based expressions:

db.Delete<Person>(where:"Age = {0}".Params(27));

Which also can take a table name so works without requiring a typed Person model

db.Delete(table:"Person",where:"Age = {0}".Params(27));

DELETE FROM "Person" WHERE Age = 27

Code-first Customer & Order example with complex types on POCO as text blobs

Below is a complete stand-alone example. No other config or classes is required for it to run. It's also available as a stand-alone unit test.

publicenumPhoneType{Home,Work,Mobile,}publicenumAddressType{Home,Work,Other,}publicclassAddress{publicstringLine1{get;set;}publicstringLine2{get;set;}publicstringZipCode{get;set;}publicstringState{get;set;}publicstringCity{get;set;}publicstringCountry{get;set;}}publicclassCustomer{publicCustomer(){this.PhoneNumbers=newDictionary<PhoneType,string>();this.Addresses=newDictionary<AddressType,Address>();}[AutoIncrement]// Creates Auto primary keypublicintId{get;set;}publicstringFirstName{get;set;}publicstringLastName{get;set;}[Index(Unique=true)]// Creates Unique IndexpublicstringEmail{get;set;}publicDictionary<PhoneType,string>PhoneNumbers{get;set;}//BlobbedpublicDictionary<AddressType,Address>Addresses{get;set;}//BlobbedpublicDateTimeCreatedAt{get;set;}}publicclassOrder{[AutoIncrement]publicintId{get;set;}[References(typeof(Customer))]//Creates Foreign KeypublicintCustomerId{get;set;}[References(typeof(Employee))]//Creates Foreign KeypublicintEmployeeId{get;set;}publicAddressShippingAddress{get;set;}//Blobbed (no Address table)publicDateTime?OrderDate{get;set;}publicDateTime?RequiredDate{get;set;}publicDateTime?ShippedDate{get;set;}publicint?ShipVia{get;set;}publicdecimalFreight{get;set;}publicdecimalTotal{get;set;}}publicclassOrderDetail{[AutoIncrement]publicintId{get;set;}[References(typeof(Order))]//Creates Foreign KeypublicintOrderId{get;set;}publicintProductId{get;set;}publicdecimalUnitPrice{get;set;}publicshortQuantity{get;set;}publicdecimalDiscount{get;set;}}publicclassEmployee{publicintId{get;set;}publicstringName{get;set;}}publicclassProduct{publicintId{get;set;}publicstringName{get;set;}publicdecimalUnitPrice{get;set;}}//Setup SQL Server Connection FactoryvardbFactory=newOrmLiteConnectionFactory(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\Database1.mdf;Integrated Security=True;User Instance=True",SqlServerDialect.Provider);//Use in-memory Sqlite DB instead//var dbFactory = new OrmLiteConnectionFactory(// ":memory:", false, SqliteDialect.Provider);//Non-intrusive: All extension methods hang off System.Data.* interfacesIDbConnectiondb=dbFactory.OpenDbConnection();//Re-Create all table schemas:db.DropTable<OrderDetail>();db.DropTable<Order>();db.DropTable<Customer>();db.DropTable<Product>();db.DropTable<Employee>();db.CreateTable<Employee>();db.CreateTable<Product>();db.CreateTable<Customer>();db.CreateTable<Order>();db.CreateTable<OrderDetail>();db.Insert(newEmployee{Id=1,Name="Employee 1"});db.Insert(newEmployee{Id=2,Name="Employee 2"});varproduct1=newProduct{Id=1,Name="Product 1",UnitPrice=10};varproduct2=newProduct{Id=2,Name="Product 2",UnitPrice=20};db.Save(product1,product2);varcustomer=newCustomer{FirstName="Orm",LastName="Lite",Email="ormlite@servicestack.net",PhoneNumbers={{PhoneType.Home,"555-1234"},{PhoneType.Work,"1-800-1234"},{PhoneType.Mobile,"818-123-4567"},},Addresses={{AddressType.Work,newAddress{Line1="1 Street",Country="US",State="NY",City="New York",ZipCode="10101"}},},CreatedAt=DateTime.UtcNow,};db.Insert(customer);varcustomerId=db.GetLastInsertId();//Get Auto Inserted Idcustomer=db.QuerySingle<Customer>(new{customer.Email});//QueryAssert.That(customer.Id,Is.EqualTo(customerId));//Direct access to System.Data.Transactions:using(vartrans=db.OpenTransaction(IsolationLevel.ReadCommitted)){varorder=newOrder{CustomerId=customer.Id,EmployeeId=1,OrderDate=DateTime.UtcNow,Freight=10.50m,ShippingAddress=newAddress{Line1="3 Street",Country="US",State="NY",City="New York",ZipCode="12121"},};db.Save(order);//Inserts 1st timeorder.Id=(int)db.GetLastInsertId();//Get Auto Inserted IdvarorderDetails=new[]{newOrderDetail{OrderId=order.Id,ProductId=product1.Id,Quantity=2,UnitPrice=product1.UnitPrice,},newOrderDetail{OrderId=order.Id,ProductId=product2.Id,Quantity=2,UnitPrice=product2.UnitPrice,Discount=.15m,}};db.Insert(orderDetails);order.Total=orderDetails.Sum(x =>x.UnitPrice*x.Quantity*x.Discount)+order.Freight;db.Save(order);//Updates 2nd Timetrans.Commit();}

Running this against a SQL Server database will yield the results below:

SQL Server Management Studio results

Notice the POCO types are stored in the very fast and VersatileJSV Format which although hard to do - is actually more compact, human and parser-friendly than JSON :)

API Overview

The API is minimal, providing basic shortcuts for the primitive SQL statements:

OrmLite API

Nearly all extension methods hang off the implementation agnostic IDbCommand.

CreateTable<T> and DropTable<T> create and drop tables based on a classes type definition (only public properties used).

For a one-time use of a connection, you can query straight of the IDbConnectionFactory with:

varcustomers=dbFactory.Run(db =>db.Where<Customer>(new{Age=30}));

The Select methods allow you to construct Sql using C# string.Format() syntax. If your SQL doesn't start with a SELECT statement, it is assumed a WHERE clause is being provided, e.g:

vartracks=db.Select<Track>("Artist = {0} AND Album = {1}","Nirvana","Heart Shaped Box");

The same results could also be fetched with:

vartracks=db.Select<Track>("select * from track WHERE Artist={0} AND Album={1}","Nirvana","Heart Shaped Box");

Select returns multiple records

List<Track>tracks=db.Select<Track>()

Single returns a single record. Alias: First

Tracktrack=db.Single<Track>("RefId = {0}",refId)

Dictionary returns a Dictionary made from the first two columns. Alias: GetDictionary

Dictionary<int,string>trackIdNamesMap=db.Dictionary<int,string>("select Id, Name from Track")

Lookup returns an Dictionary<K, List<V>> made from the first to columns. Alias: GetLookup

Dictionary<int,List<string>>albumTrackNames=db.Lookup<int,string>("select AlbumId, Name from Track")

List returns a List of first column values. Alias: GetList

List<string>trackNames=db.List<string>("select Name from Track")

HashSet returns a HashSet of distinct first column values. Alias: GetHashSet

HashSet<string>uniqueTrackNames=db.HashSet<string>("select Name from Track")

Scalar returns a single scalar value. Alias: GetScalar

vartrackCount=db.Scalar<int>("select count(*) from Track")

All Insert, Update, and Delete methods take multiple params, while InsertAll, UpdateAll and DeleteAll take IEnumerables. GetLastInsertId returns the last inserted records auto incremented primary key.

Save and SaveAll will Insert if no record with Id exists, otherwise it Updates. Both take multiple items, optimized to perform a single read to check for existing records and are executed within a sinlge transaction.

Methods containing the word Each return an IEnumerable and are lazily loaded (i.e. non-buffered).

Selection methods containing the word Query or Where use parameterized SQL (other selection methods do not). Anonymous types passed into Where are treated like an AND filter.

vartrack3=db.Where<Track>(new{AlbumName="Throwing Copper",TrackNo=3})

Query statements take in parameterized SQL using properties from the supplied anonymous type (if any)

vartrack3=db.Query<Track>("select * from Track Where AlbumName = @album and TrackNo = @trackNo",new{album="Throwing Copper",trackNo=3})

GetById(s), QueryById(s), etc provide strong-typed convenience methods to fetch by a Table's Id primary key field.

vartrack=db.QueryById<Track>(1);vartrack=db.Id<Track>(1);//Alias: GetByIdvartracks=db.Ids<Track>(new[]{1,2,3});//Alias: GetByIds

Limitations

For simplicity, and to be able to have the same POCO class persisted in db4o, memcached, redis or on the filesystem (i.e. providers included in ServiceStack), each model must have a single primary key, by convention OrmLite expects it to be Id although you use [Alias("DbFieldName")] attribute it map it to a column with a different name or use the [PrimaryKey] attribute to tell OrmLite to use a different property for the primary key.

You can still SELECT from these tables, you will just be unable to make use of APIs that rely on it, e.g. Update or Delete where the filter is implied (i.e. not specified), all the APIs that end with ById, etc.

Workaround single Primary Key limitation

A potential workaround to support tables with multiple primary keys is to create an auto generated Id property that returns a unique value based on all the primary key fields, e.g:

publicclassOrderDetail{publicstringId{get{returnthis.OrderId+"/"+this.ProductId;}}publicintOrderId{get;set;}publicintProductId{get;set;}publicdecimalUnitPrice{get;set;}publicshortQuantity{get;set;}publicdoubleDiscount{get;set;}}

More Examples

In its simplest useage, OrmLite can persist any POCO type without any attributes required:

publicclassSimpleExample{publicintId{get;set;}publicstringName{get;set;}}//Set once before use (i.e. in a static constructor).OrmLiteConfig.DialectProvider=SqliteDialect.Provider;using(IDbConnectiondb="/path/to/db.sqlite".OpenDbConnection()){db.CreateTable<SimpleExample>(true);db.Insert(newSimpleExample{Id=1,Name="Hello, World!"});varrows=db.Select<SimpleExample>();Assert.That(rows,Has.Count(1));Assert.That(rows[0].Id,Is.EqualTo(1));}

To get a better idea of the features of OrmLite lets walk through a complete example using sample tables from the Northwind database. _ (Full source code for this example is available here.) _

So with no other configuration using only the classes below:

[Alias("Shippers")]publicclassShipper:IHasId<int>{[AutoIncrement][Alias("ShipperID")]publicintId{get;set;}[Required][Index(Unique=true)][StringLength(40)]publicstringCompanyName{get;set;}[StringLength(24)]publicstringPhone{get;set;}[References(typeof(ShipperType))]publicintShipperTypeId{get;set;}}[Alias("ShipperTypes")]publicclassShipperType:IHasId<int>{[AutoIncrement][Alias("ShipperTypeID")]publicintId{get;set;}[Required][Index(Unique=true)][StringLength(40)]publicstringName{get;set;}}publicclassSubsetOfShipper{publicintShipperId{get;set;}publicstringCompanyName{get;set;}}publicclassShipperTypeCount{publicintShipperTypeId{get;set;}publicintTotal{get;set;}}

Creating tables

Creating tables is a simple 1-liner:

using(IDbConnectiondb=":memory:".OpenDbConnection()){constbooloverwrite=false;db.CreateTables(overwrite,typeof(Shipper),typeof(ShipperType));}/* In debug mode the line above prints:	DEBUG: CREATE TABLE "Shippers" 	( "ShipperID" INTEGER PRIMARY KEY AUTOINCREMENT,  "CompanyName" VARCHAR(40) NOT NULL,  "Phone" VARCHAR(24) NULL,  "ShipperTypeId" INTEGER NOT NULL,  CONSTRAINT "FK_Shippers_ShipperTypes" FOREIGN KEY ("ShipperTypeId") REFERENCES "ShipperTypes" ("ShipperID") 	);	DEBUG: CREATE UNIQUE INDEX uidx_shippers_companyname ON "Shippers" ("CompanyName" ASC);	DEBUG: CREATE TABLE "ShipperTypes" 	( "ShipperTypeID" INTEGER PRIMARY KEY AUTOINCREMENT,  "Name" VARCHAR(40) NOT NULL 	);	DEBUG: CREATE UNIQUE INDEX uidx_shippertypes_name ON "ShipperTypes" ("Name" ASC);	*/

Transaction Support

As we have direct access to IDbCommand and friends - playing with transactions is easy:

inttrainsTypeId,planesTypeId;using(IDbTransactiondbTrans=db.OpenTransaction()){db.Insert(newShipperType{Name="Trains"});trainsTypeId=(int)db.GetLastInsertId();db.Insert(newShipperType{Name="Planes"});planesTypeId=(int)db.GetLastInsertId();dbTrans.Commit();}using(IDbTransactiondbTrans=db.OpenTransaction(IsolationLevel.ReadCommitted)){db.Insert(newShipperType{Name="Automobiles"});Assert.That(db.Select<ShipperType>(),Has.Count(3));dbTrans.Rollback();}Assert.That(db.Select<ShipperType>(),Has.Count(2));

CRUD Operations

No ORM is complete without the standard crud operations:

//Performing standard Insert's and Selectsdb.Insert(newShipper{CompanyName="Trains R Us",Phone="555-TRAINS",ShipperTypeId=trainsTypeId});db.Insert(newShipper{CompanyName="Planes R Us",Phone="555-PLANES",ShipperTypeId=planesTypeId});db.Insert(newShipper{CompanyName="We do everything!",Phone="555-UNICORNS",ShipperTypeId=planesTypeId});vartrainsAreUs=db.First<Shipper>("ShipperTypeId = {0}",trainsTypeId);Assert.That(trainsAreUs.CompanyName,Is.EqualTo("Trains R Us"));Assert.That(db.Select<Shipper>("CompanyName = {0} OR Phone = {1}","Trains R Us","555-UNICORNS"),Has.Count(2));Assert.That(db.Select<Shipper>("ShipperTypeId = {0}",planesTypeId),Has.Count(2));//Lets update a recordtrainsAreUs.Phone="666-TRAINS";db.Update(trainsAreUs);Assert.That(db.GetById<Shipper>(trainsAreUs.Id).Phone,Is.EqualTo("666-TRAINS"));//Then make it disappeardb.Delete(trainsAreUs);Assert.That(db.GetByIdOrDefault<Shipper>(trainsAreUs.Id),Is.Null);//And bring it back againdb.Insert(trainsAreUs);

Performing custom queries

And with access to raw sql when you need it - the database is your oyster :)

//Select only a subset from the tablevarpartialColumns=db.Select<SubsetOfShipper>(typeof(Shipper),"ShipperTypeId = {0}",planesTypeId);Assert.That(partialColumns,Has.Count(2));//Select into another POCO class that matches the sql resultsvarrows=db.Select<ShipperTypeCount>("SELECT ShipperTypeId, COUNT(*) AS Total FROM Shippers GROUP BY ShipperTypeId ORDER BY COUNT(*)");Assert.That(rows,Has.Count(2));Assert.That(rows[0].ShipperTypeId,Is.EqualTo(trainsTypeId));Assert.That(rows[0].Total,Is.EqualTo(1));Assert.That(rows[1].ShipperTypeId,Is.EqualTo(planesTypeId));Assert.That(rows[1].Total,Is.EqualTo(2));//And finally lets quickly clean up the mess we've made:db.DeleteAll<Shipper>();db.DeleteAll<ShipperType>();Assert.That(db.Select<Shipper>(),Has.Count(0));Assert.That(db.Select<ShipperType>(),Has.Count(0));

Other notable Micro ORMs for .NET

Many performance problems can be mitigated and a lot of use-cases can be simplified without the use of a heavyweight ORM, and their config, mappings and infrastructure. As performance is the most important feature we can recommend the following list, each with their own unique special blend of features.

  • Dapper - by @samsaffron and @marcgravell
    • The current performance king, supports both POCO and dynamic access, fits in a single class. Put in production to solve StackOverflow's DB Perf issues. Requires .NET 4.
  • PetaPoco - by @toptensoftware
    • Fast, supports dynamics, expandos and typed POCOs, fits in a single class, runs on .NET 3.5 and Mono. Includes optional T4 templates for POCO table generation.
  • Massive - by @robconery
    • Fast, supports dynamics and expandos, smart use of optional params to provide a wrist-friendly api, fits in a single class. Multiple RDBMS support. Requires .NET 4.
  • Simple.Data - by @markrendle
    • A little slower than above ORMS, most wrist-friendly courtesy of a dynamic API, multiple RDBMS support inc. Mongo DB. Requires .NET 4.

About

ServiceStack.NET OrmLite - Light, simple and fast convention-based POCO ORM

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors