I want to write a Mono application which queries an SQLite database using LINQ. I tried to adapt the code from the supplied LINQ to SQL example, but couldn’t get my app to work. So I wrote a test app in C# using Monodevelop, and created the mapping using SQLmetal, and then imported the code using Oxidiser. The C# version works, but the Oxygene version throws an exception “The type initializer for ‘Mono.Data.Sqlite.SqliteConvert’ threw an exception.” - “Value does not fall within the expected range.” which I think is because the query doesn’t return any results.
Can anyone tell me what I’m doing wrong, or offer me a simple working example?
Steve
My database:
CREATE TABLE myTable
(
myIndex INTEGER NOT NULL,
myInteger INTEGER NOT NULL,
myReal REAL NOT NULL,
PRIMARY KEY(myIndex)
)
The C#:
using System;
using System.Data.Linq;
using Mono.Data.Sqlite;
using System.Linq;
namespace ConsoleLINQ
{
class MainClass
{
public static void Main (string[] args)
{
var conn = new SqliteConnection(
“DbLinqProvider=Sqlite;” +
“Data Source=test.db3”
);
var db = new DataContext(conn);
var res = from n in db.GetTable<MyTable>()
where n.MyInteger > 0
select n;
Console.WriteLine ("Result: "+res.First().MyInteger.ToString());
}
}
type
ConsoleApp = class
public
class method Main(args: array of String);
end;
implementation
class method ConsoleApp.Main(args: array of String);
begin
var conn := new SqliteConnection(‘DbLinqProvider=Sqlite;’ + ‘Data Source=test.db3’);
var db := new DataContext(conn);
var res := from n in db.GetTable() where n.MyInteger > 0 select n;
Using the following code on Mono 2.10.8 I get an “Invalid Operation Exception” when the table is empty. When there’s data I get Result!: 2.
What am I missing?
namespace ConsoleLINQ;
interface
uses
System.Data.Linq,
Mono.Data.Sqlite,
DbLinq.Data.Linq,
DbLinq.Sqlite,
System.Linq;
type
ConsoleApp = class
public
class method Main(args: array of String);
end;
[System.Data.Linq.Mapping.TableAttribute]
MyTable = public class
public
[System.Data.Linq.Mapping.Column()]
property MyInteger: Integer;
end;
implementation
class method ConsoleApp.Main(args: array of String);
begin
var conn := new SqliteConnection('DbLinqProvider=Sqlite;' + 'Data Source=db.sqlite');
var db := new DataContext(conn);
var res := from n in db.GetTable() where n.MyInteger > 0 select n;
Console.WriteLine('Result!: ' + res.First().MyInteger.ToString())
end;
end.
I’ve changed my mapping so that it looks like the way you’ve done it, rather than the way that SQLmetal/Oxidiser did it (and which works fine in C#/Monodevelop) and I get the same error as before.
I’ve attached my project; perhaps you could tell me what I’m doing wrong, please?
I’m using Windows 7 x64. And I thought I was using the latest mono, but it seems I had 2.10.3 installed. I’ve now installed 2.10.8, but it hasn’t made any difference.
I think I see what the problem is; if I run the program from the command line by calling mono, it works fine. I’ve been running it in the VS2010 debugger; presumably this means it runs with .NET rather than mono, which is why it fails?
I’ve installed Mono Tools for Visual Studio, which at least lets me run the app in mono from within VS2010, but using it to debug immediately gives a NPE in Novell.MonoVS.ProgramStart…ctor. Lack of a debugger is going to be a bit of a problem…
do you believe this is a general issue with Mono Tools for Visual Studio, or int the integration with Oxygene, specifically?
FTR, we do not officially support Mono Tools for Visual Studio, as of now, but fo you can send us details (ideally to support@), we can chase this up with the Mono/VS guys and see if they want to work with us to make Mono Tools for Visual Studio compatible with Oxygene…
It seems to be the integration with Oxygene, as it works with a C# project. I’ll send you the details.
I appreciate that this is an unusual situation (I always seem to hit those) caused by wanting to use a feature in Mono that isn’t in .NET, and other than this debugger issue, it does work fine in Oxygene.