Daily Archives: January 20, 2007

Thanks, Scott Guthrie!

When we attended the ASPInsiders Summit in December, I was impressed with the amount of time that many of the ASP.NET Team members spent with us over the three days (and nights) that we were there. Notably were Rich Ersek and Scott Guthrie, but really so many others that spent lots of time with us.

In one of the of the presentations that Scott did for us, we were reminded that he is a coder first and a manager second as he talked about working out some coding concepts on a plane but getting cut short because his laptop battery died.

One of the things that has always been great about Scott is that regardless of his elevation to higher and higher ranks at Microsoft, he’s still, in many ways, just one of the guys, or should I say, just one of the geeks.

I have always been under the assumption that Scott, who is a very busy guy, has some of his lengthy technical posts and tutorials ghost written, but it just isn’t true! He wrote about this in his rendition of "five things you didn’t know about me…" that has been going around the blogosphere.

5) I write all of the blog posts and samples on my blog myself.  A lot of people often ask if I have help doing them – but I actually write all of the posts/tutorials entirely myself (hence the reason I usually post between 10pm and 2am at night <g>).  I’ve posted 217 blog posts over the last 12 months and have responded to ~6500 (non-spam) blog comments this year.  It has kept me busy, but I also find it a lot of fun.

You might also assume that this is just part of his job, but have you ever noticed when his posts are posted? 11:30 pm. 1am. 8:30 pm. etc.

And it just makes me wonder how he does it all, but only adds to the enigma that is Scott. All I can say is that I know I’m one of many many grateful developers!

Grokking Entity Mapping in ADO.NET Orcas

ADO.NET Orcas brings forth a LOT of new concepts, especially for anyone unfamiliar with Object Relational Mapping.

One of the points of confusion is how entities are wired to the database. Now that I understand it, I thought I would use my own way of explaining it to help anyone who might have a hard time with the more deeply technical whitepapers out there.

If you were to follow the typical scenario that is demonstrated in the Entity Data Modeler Hands on Lab, this screencast or this Channel 9 video, you see that you can create an entity set by merely pointing at a database. An entity is generated for each table in the database with a one to one relationship. This isn’t really the optimal scenario for entities, but it’s a great place to get started.

Even if you haven’t installed any of the CTPs, you can look at the various XML files from the October Samples to see what these files look like. I also suggest taking a look at the Next Generation Data Access article on MSDN online for some good visuals to help grok this stuff.

Three XML files are created by the EDM tool.

  1. An XML file that represents the schema of the entities. This has the extension of CSDL.
  2. An XML file representing the schema of the database that you began with. This has an SSDL extension.
  3. An XML file that shows the mapping between elements from the CSDL file and elements in the SSDL file. It’s extension is MSL.

The SSDL file that shows the db schema contains:

An EntityContainer which represents the schema name in the db, such as "dbo". The entity container has elements that are EntitySets. These represent the tables in the database.

EntityTypes are siblings to the EntityContainer element and they represent each table and the schema of the table.

The CSDL that shows the entity schema is somewhat similar:

There is an EntityContainer which has EntitySets and AssociationSets as Children.

Then various EntityTypes flesh out the schemas of each of the EntitySets that live within the Entity Container.

Associations define the relationships between the various entity types.

So if you simply create an entity model directly from the db, the SSDL and CSDL files will look somewhat alike.

Lastly, there is the MSL file, which maps elements of the SSDL to the CSDL files.

Although there is a lot going on in the MSL file, the field mapping happens within EntityTypeMapping elements. Inside of there is a TableMappingFragment element. If you have an entity that is traversing various tables to get it’s data, you would have one EntityTypeMapping for each relationship to a table. Each EntityTypeMapping for this entity will have the same TypeName parameter so that they can be stitched together to create the entire entity.

So if you have an entity such as SalesPerson which gets info from a contacts table, an address table and a HiringInfo table, you would have three separate EntityTypeMapping elements. The first would have a TableMappingFragment that points to the Contacts table. The second points to the Address table and the third to the HiringInfo table. Within each fragment, now we can map the entity’s field to that particular table’s field.

Note: You can create fields in your entity that don’t have anything to map to directly in the db. I still have to learn how these are populated.

Then there’s the magic 🙂

So once these exist, ADO.NET Orcas does a lot of stuff in the background for us. It creates (extensible) classes from the entities that we can code against and when we request data it uses our mapping definitions to pull the correct data from the database. When we want to update, it again, uses the mapping definitions to decide what goes where (and does it very efficiently as you can witness with something like the SQL Server Query profiler). All of this happens in the background, while all we need to interact with is our nicely structured entities.