.NET 4.0 + Linq + SQLite + DbLink - how do I make it all work together?

1.5k views Asked by At

I'm developing (or trying to develop) a project in C# which stores data in an SQLite database. I'd like to use Linq for object-relational mapping. According to what I've read it's possible to use it with SQLite if you download System.Data.SQLite and dblinq.

And although it mostly works for reading data from the DB, I get an SQL syntax error when trying to insert anything. I've checked in logs that Linq is attempting to execute such a query:

{INSERT INTO [sometable]([columnone], [columntwo])
VALUES (@p0, @p1)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input String (Size = 4000; Prec = 0; Scale = 0) [foo]
-- @p1: Input String (Size = 4000; Prec = 0; Scale = 0) [bar]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
}

It seems to be an SQL query for MS Server, not SQLite. The upsetting part is this SqlProvider(Sql2008). Shouldn't it be replaced by something provided by dblinq?

My database class looks like this:

[Database]
class MyDatabase : DataContext
{
    public LibraryDatabase()
        : base(new SQLiteConnection(
                    "DbLinqProvider=Sqlite; " +
                    "Data Source=database.s3db;"))
    {
    }

    public Table<SomeObject> SomeTable;
}

The connection string is taken from the dblinq wiki.

Why doesn't it work? Is it because I'm using .NET 4.0? Has anyone successfully used such a combination? Is there another way to replace the Linq Provider?

EDIT: The table mapping: (though I doubt the error is here, as reading the table (using a foreach loop) seems to work)

[Table(Name="sometable")]
class Element
{
    [Column(Name = "id", IsPrimaryKey = true, IsDbGenerated=true)]
    public int Id { get; set; }

    [Column(Name="columnone")]
    public string PropertyOne {get; set;}

    [Column(Name="columntwo")]
    public string PropertyTwo {get; set;}
}

All the relations between tables were commented out for testing (doesn't help). An element is then inserted with the following code:

myDatabase.SomeTable.InsertOnSubmit(element);
myDatabase.SubmitChanges();
1

There are 1 answers

0
Patrick On BEST ANSWER

I'm guessing you're using Entity Framework for the ORM?

In your MyDatabase class, you are inheriting from DataContext. This Represents the main entry point for the LINQ to SQL framework. This works well for SQL Server. Not so well with SQLite.

Instead, inherit from the ObjecContext class. This provides facilities for querying and working with entity data as objects. You'll probably have to modify the code that you use for your queries but it should be pretty straight forward.

Do some searching on Entity Framework and ObjectContext, there are a bunch of good examples.

Patrick.