Sunday 10 February 2013

OS X - Super Simple Database Application with C# in 5 steps

There have been a couple of recent developments in the Mono world that have made creating applications on OS X even easier.

One useful tool that has recently appeared on the scene is Nuget. Visual Studio developers have had access to nuget for many years and now and finally it is available to Mono users. It's made very easy to use via Matt Ward's MonoDevelop nuget add in.

This blog post will walk through using Nuget on MonoDevelop on OS X to create a simple database application.

Install the Nuget Addin into MonoDevelop, there are instructions here, I added a new repository via the Add-in repository manager that points to

http://mrward.github.com/monodevelop-nuget-addin-repository/3.0.5/main.mrep

1) Create a new Console Application


File -> New -> Solution -> Select C# and then Console Project

2) Create the model


The model is a simple POCO that will map directly to the database:


Nothing complicated here.


3) Use Nuget to add ServiceStack.Net OrmLite

There are many Object Relational Mappers out there for C#, for example Mono added Microsoft's Entity Framework last year shortly after it became open source. I'll blog about using Entity Framework in the future but for this post I'm going to use an ORM called ServiceStack.Net OrmLite to access a SQLite database.


Goto Project -> Manage Nuget Packages

Add the following package:

  • ServiceStack.OrmLite.Sqlite.Mono

Use the search box to locate the package and hit the Add button to add the package to your project:




4) Add a couple of additional references.


In order to use Linq to query the database a few additional references are required, add these by right clicking on the references folder and selecting Edit References...

  • System.Core
  • System.Data
  • System.Data.Linq


5) Use ORMLite to create the database, populate it with data and query it


When run:

The code for this blog post is available for download here.

No comments:

Post a Comment