Using Teleriks new LINQ implementation to connect to MySQL

Posted on Dot net Slackers See other posts from Dot net Slackers
Published on Tue, 30 Mar 2010 00:00:00 GMT Indexed on 2010/03/30 18:43 UTC
Read the original article Hit count: 370

Filed under:

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.

image

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.

image

When the wizard comes up, choose MySQL as your back end and enter in the name of your saved MySQL connection.

image

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.

image

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.

image

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: }

F5 produces the following.

image

Tomorrow Ill show how to take the same model and create an Astoria/OData data feed.

Technorati Tags: MySQL
Bookmark and Share

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.



Email this Article

© Dot net Slackers or respective owner