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, Teleriks Visual Entity Designer is a new
way to build your domain model that is very powerful and also real easy to use. How
easy? Ill 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 dont 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, dont 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 dont 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 anOpenAccess using statement 4: 5: namespace ConsoleApplication3 6: { 7: class Program 8: { 9: static void Main(string[]
args) 10: { 11: //a reference tothe 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("CompanyName: " + cust.CompanyName); 22: } 23: //keep the consolewindow open 24: Console.Read(); 25: } 26: } 27: }
.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; }
Lines 19-24 loop through the result of our LINQ query and displays the results.
Thats 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!
Technorati
Tags: Telerik,OpenAccess,LINQ
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.