Category Archives: Data Access

What to expect in next (and future) Orcas bits for Entity Framework

I am at DevConnections where I was VERY excited to see Brian Dawson (ADO.NET team) do not one but TWO Entity Framework talks on Monday.

Brian has a newer build than what we are working with in the March CTP and I saw some stuff that made me wanna whine “gimme gimme”.

  • SPAN – calling span on an entity sql query will force the ObjectContext to load up an object’s entire hieriarchy without having to call Load after the fact to get related data. This seems to be available in Object Services but not through Linq to Entities. Hopefully an Extension Method will be created for Linq to Entities to give us access.
  • The EDM Wizard will display Views and Stored Procedures when building an EDM from a database. Then these will be part of the schemas.
  • Referential Contraints will work the way you would expect. For example if your db has a referential constraint to delete child records when a parent is deleted, EDM will pick those up. I don’t know the details here but am assuming we will have some granular control over this.
  • ToList may not be necessary in the future and serialization will be implicit.
  • Beta 2 will have something (new to me) called IPOCO… Interface for Plain Old CLR Objects
  • QueryViews – ooh baby ooh baby. Create your own views in the mapping layer.
  • Associations between sub types.
  • There will be a way to convert existing strongly typed datasets to entity schemas. Unfortunately either Erick (who I got this from) misunderstood the question or I misunderstood the question or I misunderstood the answer. Darn.

At the same time as DevConnections, VSLive was happening in San Francisco. (Quite unfortunate scheduling…) Britt Johnston did a keynote and showed [a video of] the latest prototype of the EDM Modeler and also let us know that it won’t be ready for Orcas but they plan to release it shortly (?) (well the quote from the ADONET blog post is literally “sometime”) after Orcas. This is really frustrating, but it is just the reality and as developers we know the difficulties of designing tools… so it is what it is and until we have it, I will learn a LOT with the XML and personally hold off on doing any seriously complex modeling.

See the ADONET Team blog and Data Blog for more on Britt’s keynote and links to the screencast on where the modeler is at today.

I did my ADO.NET Orcas overview talk yesterday to a full room (not a huge room, but still all the seats were filled which was great given that Carl Franklin and Richard Campbell were doing a live DotNet Rocks show with Scott Guthrie at the same time!). I love talking about this stuff even if I never seem to have enough time for all the cool stuff I wish I could show.

Recent Presentations on what’s new in ADO.NET Orcas

Last week, I inaugurated my ADO.NET Orcas Overview at the TEchValley.NET User Group in Albany NY (with thanks to INETA!!). It was a serendipitous night for me to present there as they had just moved to new digs for their meeting, a very convenient, spacious and high tech location, so they packed the room!

The purpose of this presentation is to introduce developers to the Entity Framework and to LINQ for ADO.NET (that’s the umbrella term for LINQ to SQL, LINQ to DataSets and LINQ to Entities) and while it sounds like a short list, it’s a LOT to cover in one session. Especially if you don’t want to just do some marketing. Developer’s want to see code, but there is a lot of high level explaining to do up front, which takes time. Yet, I can’t help wanting to desconstruct the Entity Data Model schema files, try to show different ways of designing a conceptual layer, and then the many ways of getting data out of the entity framework as well as using LINQ to SQL (a huge topic all on it’s own) and LINQ to DataSets (another good sized topic, if you like DataSets, which I do!). Minimally, a day would be good to start with.

Before I opened up the schema files, I asked “so, who here is comfortable working with XML, anyway?”. I was surprised that 1/3 of the hands went up. This is a smart group of developers who challenged me with a lot of awesome questions! (Though there was a big sigh of relief when I mentioned LINQ to XML and LINQ to XSD for those of use who live in fear of XPath!)

I laughed at John Papa’s recent blog post where he bemoans the difficulty of cramming the same list of info into an article he is writing for MSDN Magazine. John and I have been providing lots of moral support to each other as we attempt to wade through Entity Framework and the LINQ flavors that are involved with data access. I was happy to finally meet John in person at the meeting, as well. (A local yokel!)

The most laughable part of my session was when, after constantly checking my watch to gauge how much time I had left (and being surprised, at each check, how well I was doing with the time), I realized that I hadn’t changed the time on my watch for the early daylight savings time. I didn’t have 1.5 hours to go, but only 1/2 hour! While I had planned to do a 2 hour session (user groups are a little more amenable to this than conferences where you are on a tight schedule), I think that, not counting the short break we took, I managed to wrap up in 2 hours and 15 minutes – and nearly everyone stayed! But what’s new? (Hey, you’ve got me there, take advantage of it! ;-))

The next day, I had a three hour drive home, immediately followed by a GeekSpeak webcast on the same topic, but for only one hour. My favorite part of this format was that whenever I was starting to go on and on about one particular piece of the Entity Framework (can’t be helped as I find it fascinating – sick, huh?), Susan would steer me to the next stage of the discussion. Boy, would I love to have Susan with me while I’m presenting at a conference. “Okay Julie, I think 5 minutes looking at XML is more than enough… let’s go look at something a little sexier, like the LINQ to SQL designer, huh?”

So next up is DevConnections, where I will be doing this session in 75 minutes (less, if I want to be able to answer questions), then on to Code Camp in Waltham, then the South Sound User Group in Olympia (after talking about LINQ to SQL In Bellingham, WA – both INETA gigs) and DevTeach in Montreal. I’m excited about all of these opportunities to introduce developers to these very cool technologies!

Talking about ADO.NET Orcas on MSDN GeekSpeak on Wednesday

GeekSpeak is a lot more free-form than a typical webcast and I’m not sure what to expect. I’ll have the new CTP of ORCAS open and I guess we’ll poke around Entity Framework and the three LINQ to ADO.NET techs (LINQ to SQL, LINQ to Entities nad LINQ to DataSet). And the most fun part is that I’ll be doing this with hosts Glen Gordon (who I did a webcast with on ADO.NET 2.0 topic a few years ago) and Susan Wisowaty (who lives right here in Burlington!), from the MSDN Events team.

More info and registration here

Sorting out LINQ and Entity Framework for Data Access

There are SO many ways to “skin the data access cat” in Orcas that I’m getting a little cross-eyed. (Though this could be from looking at all of the XML in the Entity Data Models.)

I thought I would try to sort it out a little here since I have done this for some of the presentations I am putting together.

ADO.NET in Visual Studio Orcas

The “new” ADO.NET is not new, but added to. Evolution and backwards compatibility are key to the ADO.NET team. So in Orcas you will have what you know as ADO.NET 2.0 basically in tact. Then added onto that are two things: Entity Framework and integration with LINQ.

LINQ Integration in ADO.NET (in VS Orcas).

Painting a broad brush stroke to define LINQ is that it gives you the ability to query in-memory objects (that are iEnumerable).

 While there is the basic LINQ to Objects syntax, there are four derivatives of this.

LINQ to XML allows you to use LINQ to query XML.

There are three others that all fall under the umbrella of LINQ to ADO.NET.

The first of these is LINQ to DataSet. DataSets are in-memory objects, right? So we can query them, too. What we are really querying is an Enumerable collection of DataRows. Because of the special nature of DataSets, this “flavor” of LINQ needs to be specialized.

Next is LINQ to SQL. Of course, this taints my brushstroke because SQL is not an in-memory object. 🙂 However, as long as we have this fabulous new language enhancement, and we are all over querying databases, this version of LINQ was created to work directly with SQL, not only to query, but to update the data as well.

The last of the LINQs is LINQ to Entities. This lets us use LINQ to query the objects that are created by the Entity Framework. So let me jump to that and then come back to this one.

Entity Framework

Entity Framework is a whole new set of APIs added into the System.Data namespace.

The key to the framework is a set of three schema files.

The first file describes the conceptual layer of your business entities using a schema file. This is not replacing your objects. All we are describing is structure and basic metadata. Two of the (many) big advantages of this are that 1) in the end, we can write our data access code against this schema (which creates classes for us) rather than having to code up connections to the db, create complex joins to pull together info from various related tables and write lots of update logic and 2) we can describe these conceptual entities based on what we want them to look like, rather than how they are best organized in a database.

The second files describes the schema of the database, including pk/fk relationships.

The third file is a map between the first two. So that when you code against the entities, the framework can translate your requests (or updates) into what the database is expecting.

So those are just three files (which can be created directly from your db using a wizard or a command line tool and then edited manually).

Interacting with the schema files

There are two APIs that know how to make these schemas do their magic, Object Services and Entity Client.

Object Services

Object Services know how to work with the entities as objects. You can query against the entities and get objects as your results. These objects are managed in memory by Object Services and you can update the database easily with changes made to the objects. So I said the magic word, query.

There are two ways to query with the object services. 1) Directly using (yet ANOTHER query syntax…) Entity SQL, which is similar to using SQL and is built using strings. Here you would use the ObjectServices API to create a query object and then pass in the Entity SQL string 2) Indirectly using LINQ to Entities. If you use LINQ to Entities to query the entity classes, in the background LINQ to Entities will use Object Services to interact with the objects. Either way, ObjectServices will maintain in-memory knowledge of the objects for updates.

There are  providers being built to interact with other databases. Regardless of the database, once the schemas and mapping are built, everytihng you do on the client side will be the same.

A caveat is that if you use Object Services plus Entity SQL to query and in your query you use projections (request specific columns/properties instead of entire objects) you will not get an updatable object. Instead you will get an IEnumerable collection of DbDataRecords. LINQ to Entities will return objects even with projections, because that’s what LINQ knows how to do.

Entity Client

The last thing I wanted to talk about is Entity Client, the other way to access entities. EntityClient is a provider, similar to SQLClient and the others. It let’s you build your data access code to query the entities in a familiar way, with connections (though the connection points to your entity objects) and commands. With EntityClient, you build queries using Entity SQL syntax. EntityClient queries return a dbDataReader. You cannot do updates directly through EntityClient. An interesting thing about this provider is that if you are building an app that leverages the Provider Independent API, you can have code that easily flips back and forth from access data in SQL Server, Oracle, Access, or other data engines. So (though I haven’t tested this, I’m making an educated guess here) you can add EntityClient into this flexible model.

Summary

So now you can see why I’m a little cross-eyed. We are working with 6 new query languages (though they are all related),5 new ways of accessing data, trying to keep track of which syntax goes with which access method and which access method returns what type of data. And it is dizzying for sure. But after some investment in this, I have definitely gotten it sorted out. And you can too!

 

Stored Procs in LINQ to SQL in March CTP (The VB Version)

In a previous interation, I managed to track down some sample code that built a LINQ call to a stored procedure, the class which it returns and executed it in LINQ to SQL.

The code has changed dramatically in the March CTP. Luckily, Mike Taulty beat me to it and I leveraged the C# code in his post to revise mine.

One of the things I was so silly to do in my previous test was to hand code the return class rather than just create it in the designer and return that.

But all of this is not totally necessary since you can now drag and drop stored procs onto the LINQ to SQL Visual Modeler and make calls to it. Interestingly the return type is just created on the fly at run time so it’s not even an issue – yay to anonymous types.

However, it’s still nice to know how to build this stuff becasue drag n drop doesn’t solve every coding problem. So check Mike’s post for the C# version.

Here is a working version in VB.

“CustOrderHist” is the name of the stored procedure in my database. 

What this is doing is

1) using the attribute to hook it up to the sproc
2) taking in a parameter of customerID and associating it with the parameter @CustomerID in the stored proceudre
3) Returning and IEnumerable of type CustOrderHistoryResult (the class I created in the designer that matches the structure of what the sproc returns).

On the client side, I can now take the result and do whatever I want with it, such as bind it to a gridview on a web page.

<StoredProcedure(Name:=”CustOrderHist”)> _
Public Function GetCustOrderHist(<Parameter(Name:=”CustomerID”)> ByVal customerID As String) As IEnumerable(Of CustOrderHistoryResult)

  Dim result As IQueryResults(Of CustOrderHistoryResult) = _
 
ExecuteMethodCall(Of CustOrderHistoryResult)(Me,
  MethodInfo.GetCurrentMethod, customerID)
    
  Return result

  End Function