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 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.
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 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)
3: if (!IsPostBack)
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;
F5 produces the following.
Tomorrow I’ll show how to take the same model and create an Astoria/OData data feed.