Love LINQ to SQL but are concerned that it is a second class citizen? Need to connect to more databases other than SQL Server? Think that the Entity Framework is too complex? Want a domain model designer for data access that is easy, yet powerful? Then the Telerik Visual Entity Designer is for you.
Built on top of Telerik OpenAccess ORM, a very mature and robust product, Telerik’s Visual Entity Designer is a new way to build your domain model that is very powerful and also real easy to use. How easy? I’ll show you here.
First Look: Using the Telerik Visual Entity Designer
To get started, you need to install the Telerik OpenAccess ORM Q1 release for Visual Studio 2008 or 2010. You don’t need to use any of the Telerik OpenAccess wizards, designers, or using statements. Just right click on your project and select Add|New Item from the context menu. Choose “Telerik OpenAccess Domain Model” from the Visual Studio project templates.
(Note to existing OpenAccess users, don’t run the “Enable ORM” wizard or any other OpenAccess menu unless you are building OpenAccess Entities.)
You will then have to specify the database backend (SQL Server, SQL Azure, Oracle, MySQL, etc) and connection.
After you establish your connection, select the database objects you want to add to your domain model. You can also name your model, by default it will be NameofyourdatabaseEntityDiagrams.
You can click finish here if you are comfortable, or tweak some advanced settings. Many users of domain models like to add prefixes and suffixes to classes, fields, and properties as well as handle pluralization. I personally accept the defaults, however, I hate how DBAs force underscores on me, so I click on the option to remove them.
You can also tweak your namespace, mapping options, and define your own code generation template to gain further control over the outputted code. This is a very powerful feature, but for now, I will just accept the defaults.
When we click finish, you can see your domain model as a file with the .rlinq extension in the Solution Explorer.
You can also bring up the visual designer to view or further tweak your model by double clicking on the model in the Solution Explorer.
Time to use the model!
Writing a LINQ Query
Programming against the domain model is very simple using LINQ. Just set a reference to the model (line 12 of the code below) and write a standard LINQ statement (lines 14-16). (OpenAccess users: notice the you don’t need any using statements for OpenAccess or an IObjectScope, just raw LINQ against your model.)
1: using System;
2: using System.Linq;
3: //no need for an
OpenAccess using statement
4:
5: namespace ConsoleApplication3
6: {
7: class Program
8: {
9: static void Main(string[] args)
10: {
11: //a reference to
the data context
12: NorthwindEntityDiagrams dat = new NorthwindEntityDiagrams();
13: //LINQ Statement
14: var result = from c in dat.Customers
15: where c.Country == "Germany"
16: select c;
17:
18: //Print out the company name
19: foreach (var cust in result)
20: {
21: Console.WriteLine("Company
Name: " + cust.CompanyName);
22: }
23: //keep the console
window open
24: Console.Read();
25: }
26: }
27: }
Lines 19-24 loop through the result of our LINQ query and displays the results.
That’s it! All of the super powerful features of OpenAccess are available to you to further enhance your experience, however, in most cases this is all you need.
In future posts I will show how to use the Visual Designer with some other scenarios. Stay tuned.
Enjoy!
Stephen Forte sits on the board of several start-ups including Triton Works. Stephen is also the Microsoft Regional Director for the NY Metro region and speaks regularly at industry conferences around the world. He has written several books on application and database development including Programming SQL Server 2008 (MS Press).