Telerik blogs

Just a few weeks ago Microsoft released Visual Studio 2010 Beta2. Last week Telerik put its Q3 release live into production. One of the cool new Q3 features is that OpenAccess now works seamlessly with Visual Studio 2010. That means you can target .NET 3.5 or .NET 4.0 using either Visual Studio 2008 (.NET 3.5) or Visual Studio 2010 (.NET 4.0).

I will do a quick demo with Visual Studio 2010, SQL Azure, and OpenAccess. With OpenAccess Q3 installed, I fired up Visual Studio 2010 and started a console project targeting .NET 4.0

image

While the project will target the .NET 4.0 Framework, we have to do one small thing to make it work. By default the project type is “.NET 4.0 Client Profile” so we have to change that to a straight up .NET 4.0 project type. The way to do this is to right click on the project and select properties. In the properties dialog Application section, you will see Target framework;  select .NET 4.0 and you are good to go. (Visual Studio will have to close and reopen the project for you.)

image

Next we have to fire up OpenAccess via the Enable Project Wizard. When I start the Enable Project to use ORM Wizard, OpenAccess asked me what database to use, and as I showed before on this blog, Q3 now supports SQL Azure natively. Notice that the wizard will prompt you to put in your SQL Azure credentials and will give you the basic template for your server name: tcp:<sqlazureid>.database.windows.net.

image

Note: Depending on your setup in Visual Studio 2010, you may have to use the Server name without the tcp: and use the syntax UserName@sqlazureid. Visual Studio 2010 will give you an error in your setup if the default does not work. If you get this error you would enter the following for your SQL Azure credentials:

Server Name: sqlazureid.database.net (no tcp:, so for example p28drog84.database.net)
User Name: YourSQLAzureUserID@sqlazureid (for example: Stevef@p28drog84)

Next you will want to map some SQL Azure tables to OpenAccess entities. This can be done pretty easily, just by running the Reverse Mapping wizard. Here you can select your tables to map. By default OpenAccess will also now map the foreign keys of each entity as a primitive type in addition to the complex type. This will help a great amount if you are using your entities in conjunction with any data service such as WCF or ADO .NET Data Services. (More on that later.)

image

Once you have mapped your entities, you are free to work with them. You can use the OpenAccess LINQ implementation (which has went through a major overhaul and is in line with the LINQ to SQL and LINQ to Entities LINQ implementations.) As I showed last time, you can write a simple LINQ statement to filter all the Customers by a certain country as shown here:

 1: static void Main(string[]
args)
 2: {
 3:  IObjectScope dat = ObjectScopeProvider1.GetNewObjectScope();
 4:  //LINQ Statement 
 5:  var result = from c in dat.Extent<Customer>()
 6:  where c.Country == "Germany"
 7:  orderby c.CompanyName
 8:  select c;
 9:  //Print out the
company name 
 10:  foreach (var cust in result)
 11:  {
 12:  Console.WriteLine("Company
Name: " + cust.CompanyName);
 13:  }
 14:  //keep the console window open 
 15:  Console.Read();
 16: }

 

image

Enjoy!


About the Author

Steve 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).

Comments

Comments are disabled in preview mode.