Q1: what is the schema for this database?
> I use sqlmetal to generate the MySampleDB.cs file as
> follows:
>
> $ sqlmetal /provider:Sqlite /conn "Data Source=./MySampleDB.sq3" /
> code:MySampleDB.cs
Q2: What is generated from this command?
> Then I moved the generated cs code to the project directory and added
> this file to my project in MonoDevelop.
>
> I edited the file to rename all "Main" in the MySampleDB.cs to be
> MySampleDB is avert confusion with my Main() class.
You shouldn't have a Main class anyway for your program entrypoint, as
Main() is the name of the program entrypoint method, and Main.Main()
would be a constructor. That way lies madness. ;-)
Meanwhile, you can have a Main type and a Foo.Main() method without
naming collision.
Finally, you can use `sqlmetal --database=MySampleDB ...` to cause
sqlmetal to generate a DataContext subclass with the name MySampleDb.
> In my Main.cs on the project, which is a console type project, I
> created the following code:
>
> using System;
> using System.Data;
> using Mono.Data.SqliteClient;
> using System.Linq;
> using DBLinq;
>
> namespace sqlinqtest
> {
> class MainCLass
> {
> public static void Main(string[] args)
> {
> linqtestcode();
> }
>
> private static void linqstylecode()
> {
> IDbConnection dbcon;
> dbcon = (IDbConnection) new SqliteConnection(
> "DBLinqProvider=Sqlite; DataSource=./
> MySampleDB.sq3" // per other blog, does not require
> //
> DbLinqDataTypeProvider if pass
This is probably a casing issue, as it should be DbLinqProvider, not
DBLinqProvider (note that 'b' should be lowercase).
You're also mentioning some blog, but not the URL for said blog, which
could provide additional context.
//
> So the problem I'm having is that the table column definitions in the
> query is not visible and obviously I get an error at compile saying
> that "Type 'User' does not contain the definition for 'uid' and no
> extension method 'uid' of type 'User' could be found", suggesting that
> I may have a missing "using" directive or a missing assembly
> reference.
No, it implies that db.User is returning a User type, which is found,
but the property reference 'u.uid' cannot be found. Based on your
sqlmetal invocation, this is likely because sqlmetal is PascalCasing
your column names, and thus it should be u.Uid and u.Name (as opposed to
u.uid and u.name).
You can use the /case:leave option to partially disable this behavior.
Again, viewing the generated C# source would be helpful here [Q2].
- Jon
So this is either a MonoDevelop code completion bug, or some other bug.
Thus, the real question is this: does the following compile?
> var users = from u in db.User select u;
If it compiles without error (which I fully expect it to), then the
problem isn't sqlmetal/Linq-to-DB/etc., it's MonoDevelop. The fact that
this compiles but similarly doesn't show code completion:
> var users = from u in db.User select (new {UserId = u.Uid, FullName =
> u.Name});
further suggests that it's a MonoDevelop bug.
> Now it is complaining that I have
> an invalid connection string. This is how I have it:
>
> IDbConnection dbcon;
> dbcon = (IDbConnection) new SqliteConnection("Data Source=./
> MySampleDB.sq3; Version=3;");
The cast shouldn't be necessary, and I don't know why that would be
failing, though I see you later fixed it by using URI (which similarly
doesn't make sense...). I know that Data Source works for me, though
I've never used Version, e.g. [0].
However, you're still missing the DbLinqProvider=Sqlite parameter, so
you should be doing:
dbcon = new SqliteConnection (
"URI=file:./MySampleDB.sq3;" +
"Version=3;" +
"DbLinqProvider=Sqlite"
);
If you omit DbLinqProvider, then the DataContext will generate
MSSQL-style SQL, which DbLinq doesn't always like.
- Jon
[0] http://www.jprl.com/Blog/archive/development/mono/2009/Mar-12.html