This content is deprecated!

July 28, 2009

LINQ, PostgreSQL and Mono

Categories: FreeBSD, Mono.

LINQ to DB is planned for mono-2.6, however I wanted to access data from a PostgreSQL database, without spending much time playing with Npgsql, and using the stable version of mono: 2.4.2.3.

Here comes DbLinq, which aims to provide linq to SQL for many DataBase systems. While this project is still considered experimental to some extends by its developers, I wanted to give it a try. Since it way quite a PITA, here are the details for building using DbLinq with mono.

Get DbLinq from the Subversion repository

The latest stable release is quite old, and the project page warns we recommend using the latest SVN HEAD, so checkout the code from the google code repository:

svn checkout http://dblinq2007.googlecode.com/svn/trunk/ dblinq2007

Open the solution in MonoDevelop (or your favourite editor) and edit each project options so that assemblies are signed with the DbLinq.snk key.

You can then compile all needed assemblies and the DbMetal utility.

Get the System.Data.Linq.dll assembly

This assembly is not yet in the stable mono releases. You can compile it from the mono trunk:

svn checkout http://anonsvn.mono-project.com/source/trunk/mcs
cd mcs && ./configure && gmake

The assembly for the 2.0 profile is then available as class/lib/net_2_0/System.Data.Linq.dll.

Start you project

Create a new project as usual and add any required references. For example, my project use PostgreSQL so I added the following assemblies:

Generate classes to access your DataBase

The DbMetal utility generate classes from a DataBase schema. Before using it, ensure that DbMetal.exe.config exists in the DbMetal.exe directory and if not, copy it from DbMetal/App.config. You can then use the utility to generate your code:

mono DbMetal.exe /conn:"server=localhost;user id=user;\
   password=password; database=db" /provider=PostgreSQL \
   /code:db.cs /pluralize

Add the generated db.cs to your project and start coding:

using System;
using Npgsql;

class MainClass
{
	public static void Main(string[] args)
	{
		string connStr = "server=localhost;user id=user; password=password; database=db";
		NpgsqlConnection conn = new NpgsqlConnection(connStr);

		using (Db db = new Db(conn))
		{
			foreach (Employees e in db.Employees)
			Console.WriteLine(e.Name);
		}
	}
}

No Comments Yet

Comments RSS feed | Leave a Reply…

top