Using Teleriks new LINQ implementation to connect to MySQL
Last week Telerik released
a new LINQ implementation that is simple to use and produces domain models very
fast. Built on top of the enterprise grade OpenAccess
ORM, you can connect to any database that OpenAccess can connect to such as: SQL
Server, MySQL, Oracle, SQL Azure, VistaDB, etc. Today I will show you how to build
a domain model using MySQL as your back end.
To get started, you have to download MySQL 5.x and the MySQL Workbench and also, as
my colleague Alexander Filipov at Telerik reminded me, make sure you install the MySQL
.NET Connector, which is available
here. I like to use Northwind, ok it gives me the warm and fuzzies, so I
ran a script to produce Northwind on my MySQL server. There are many ways you can
get Northwind on your MySQL database, here is a helpful
blog to get your started. I also manipulated the first record to indicate that
I am in MySQL and gave a look via the MySQL Workbench.
Ok, time to build our model! Start up the Domain Model wizard by right clicking on
the project in Visual Studio (I have a Web project) and select Add|New Item and choose
Telerik OpenAccess Domain Model from the new item list.
When the wizard comes up, choose MySQL as your back end and enter in the name of your
saved MySQL connection.
If you dont have a saved MySQL connection set up in Visual Studio, click on New
Connection and enter in the proper connection information. *Note, this is where you
need to have the MySQL .NET connector installed.
After you set your connection to the MySQL database server, you have to choose which
tables to include in your model. Just for fun, I will choose all of them.
Give your model a name, like NorthwindEntities and click finish. That is it.
Now lets consume the model with ASP .net. I created a simple page that also has a
GridView on it. On my page load I wrote this code, by now it should look very familiar,
a simple LINQ query filtering customers by country (Germany) and binding the results
to the grid.
1: protected void Page_Load(object sender,
EventArgs e)
2: {
3: if (!IsPostBack)
4: {
5: //a reference to
the data context
6: NorthwindEntities dat = new NorthwindEntities();
7: //LINQ Statement
8: var result = from c in dat.Customers
9: where c.Country
== "Germany"
10: select c;
11: //Databinding to
the Gridview
12: GridView1.DataSource = result;
13: GridView1.DataBind();
14: }
15: }
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
F5 produces the following.
Tomorrow Ill show how to take the same model and create an Astoria/OData data feed.
Technorati
Tags: MySQL
Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.