LINQ-to-MySQL with DbLinq in C# - Preliminary Tasks
(Page 2 of 4 )
First you need to download the latest release of DbLinq. As we discussed in our previous article, both the source code and the archived (.zip) versions can be downloaded via Google Code. The project is featured as dblinq2007. For now you should download the archived version because it contains the binaries as well.
Take a minute or two to familiarize yourself with the structure of DbLinq (such as its folders and files). You are going to find DbMetal.exe under the build folder. That is the file that generates the C# class from the database schema and mapping attributes (ORM). It is a command-line utility; currently there is a GUI version of it n development. Under the srcDbMetal folder you are going to find numerous batch files as examples.
Those aforementioned batch files are called something like run_myMetal.bat; that suggests it gives an example of how to run the DbMetal utility with its arguments in the case of a MySQL server. The same applies for run_oraMetal.bat, run_sqliteMetal.bat, and so forth. Now we are going to copy the run_myMetal.bat into the build folder where the DbMetal executable is to be found, and edit the batch file accordingly.
DbMetal.exe -provider=MySql -database:your_db -server:your_server -user:root -password:your_pass -namespace:namespace -code:filename.cs -sprocs
Running the batch file will create the filename.cs C# class that "describes" your MySQL database along with its tables, and has the necessary resources so that you can work on it later and implement the provider. Now you need to copy the following files into the directory of your project and include them into your project:
The output of the DbMetal.exe in the case of successful execution is the following:
DbLinq Database mapping generator 2008 version 0.18.0.0
for Microsoft (R) .NET Framework version 3.5
Distributed under the MIT license (http://linq.to/db/license)
>>> Reading schema from MySQL database
<<< writing C# classes in file 'hotelsdb.cs'
Should the script (batch file) fail, then there are problems with the arguments you specified in the batch file, such as privileges, invalid password, and so forth. Moving on, when you first include the generated C sharp class file, you may eventually compile the project, and if there are errors, let's fix those. There won't be any algorithm-related problems, but rather issues like ambiguous references, some objects that cannot be resolved, etc.
For example, in my case, there were ambiguous references between the following:
System.Data.Linq.Table<hotels_Clients> and DbLinq.Data.Linq.Table<hotels.Clients>
And the error was found in the following snippet:
public Table<Clients> Clients { get { return GetTable<Clients>(); } }
As you can see, the IDE couldn't resolve the references. The type "Table" is to be found in both "System.Data.Linq" and "DbLinq.Data.Linq." The first is the native LINQ while the latter is our DbLinq provider. Therefore, to alleviate this ambiguous reference we can easily rewrite the code as follows (this is one of many solutions, though):
public DbLinq.Data.Linq.Table<Clients> Clients { get { return GetTable<Clients>(); } }
Anyway, with basic knowledge of C# and knowing its syntax, you shouldn't have any problems getting around these errors. In a nutshell, the entire database schema is mapped into the class. Another error that may happen is that, within the using directives at the beginning of the class, the following cannot be resolved: "DbLinq.Linq" and "DbLinq.Linq.Mapping." Write them as DbLinq.Data.Linq and DbLinq.Data.Linq.Mapping. Simply put, the structure differs a bit.
Next: Let's Do It! >>
More C# Articles
More By Barzan "Tony" Antal