# Thursday, 25 March 2010

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 don’t 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 let’s 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 I’ll show how to take the same model and create an Astoria/OData data feed.

Technorati Tags:

Bookmark and Share
Thursday, 25 March 2010 08:17:32 (Eastern Standard Time, UTC-05:00)
Just thought I should point out that you can use MySQL with the (free) Entity Framework. I'm not knocking Open Access ORM, as I haven't used it, but it's heard to beat free!

I've used MySQL and Entity Framework in project and it worked perfectly. All you need to do to get them working together is just install the MySQL Data connector so you have the DLL's and so Visual Studio Entity Framework Designer shows MySQL as a database option in the connection dialog. It's practically seamless!

For posterity, I thought I'd also mention that SQLYOG is a good free MySQL GUI tool, but I haven't tried the WorkBench before. Going to try it now!
Comments are closed.