Monthly Archives: May 2008

Presenting on the bleeding edge

When VS2008 SP1 beta was released on Monday, I was getting ready to head for Toronto to present three sessions at DevTeach which would be on Wednesday and Thursday. One session was on Astoria another touched LINQ to SQL/EF and DataSets and the last was a deep dive into EF with respect to tiered programming.

The new bits would impact these talks, especially the Astoria and deep dive EF talk a lot and I truly did not want to show people what were now incorrect namespaces, classes whose names had changed, Entity Graphs that did not serialize with services, etc etc.

I made a scary decision and took a huge risk. I updated all of my demos to align with SP1 – this  I did on my dev box at home. This was more than a matter of tweaking some EDMX files and changing class names. The tiered applications needed to be completely reconsidered, rearchitected and rewritten. When I had gotten far enough in this process that I was confident I could finish the transition up, I knew it was time to get the SP1 bits onto my presentaiton laptop.

This didn’t go so well. I started the install just before I headed off to my user group meeting. Six hours later I returned to see the install failed because the vista windows sidebar was on. This was 10:30pm. I worked at trying to get the new bits installed until 9am the next morning and then had to give up and head for the airport. To be safe, I threw my CPU, which had what I needed, into my suitcase!

At the speaker dinner that night in Toronto, I was offered a VPC by scott Hanselman who is my savior for the month of May. It already had Vista on it, with VS2008, the Service pack and even Fiddler which I needed for the Atoria session. It was a full day since I began this installation. I hadn’t slept and I hadn’t been able to complete the demo transitions but I was fully committed.

Or perhaps should be committed.

I pushed my EF debugging skills full bore. But there was more to deal with since the VPC wasn’t quite the same as my setup. I had to install all of my databases and change all of the connection strings (there were a lot) inthe many many projects for the 3 sessions in addition to running thorugh every single demo to see where things weren’t working.

I didn’t get everything fixed on  time and had to go into my presentations knowing that I’d have to skip a few demos and hoped that the bulk of the attendees would feel the same as I did about being able to see how things are not how things were. I think the feelings were mixed (sorry to anyone who found my decision not to be the best).

There is still one entity sql query that is driving me nuts. It keeps saying that “customer” doesn’t exist, though the query looks at Customers. Almost as though I had cached something.

I got a few hours of sleep, had chocolate and coffee as my critical helpers, bit the bullet and went for it.

But boy oh boy, I’m not sure if I really need this kind of excitement in my life! 🙂

I still need to tighten the rest up so I can get the demos up ontot he DevTeach website, but since not many of the session attendees have SP1 installed yet, hopefully they won’t mind waiting a few days.

Notes from updating an Astoria project to the new SP1 Beta bits

The Astoria Team has provided a very impressive list of what’s changed and what’s new in the new bits for Astoria that were released with VS2008 SP1 today.

I was surprised to see they already have the Batching and Optimistic Concurrency stuff in there that they were only just recently considering.

Here are my own notes from trying to get an Astoria demo working before I had access to the nice list that is now available.

1) If you are using an Entity Framework EDM as your data source , there are some easy to fix (and easy to discover) breaking changes to the model.

See this post on my notes for updating an Entity Framework solution to SP1.

2) The service file is very different. You will be best to create it from scratch then move your logic into the new SVC file.

3) Namespaces and classes have changed.

  • Microsoft.Data.WebClient namespace becomes System.Data.Services
  • WebDataContext becomes DataServiceContext
  • WebDataQuery becomes DataServiceQuery

4) Methods for limiting access and the enums have changed

OLD config.SetResourceContainerAccessRule("*", ResourceContainerRights.AllRead);
NEW config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

SetServiceOperationAccessRule stays the same

5) WebDataGen is now DataSvcUtil and can be found in C:\Windows\Microsoft.NET\Framework\v3.5\DataSvcUtil. The parameters for this were simplified, too.

I recommend using DataSvcUtil to recreate any classes you want to use in Astoria clients. I actually fixed one up manually before I found DataSvcUtil and it was a pain.

6) The Attribute used to identify the Key field (needed when the field names are not discoverable by Astoria) has changed.

It was <System.Web.DataWebKey()> and was used on the property.

Now it is System.Data.Services.Common.DataServiceKey(propertyname) and is used on the class. It can also take an array of strings if there are multiple keys.

7) The Silverlight and AJAX client libraries are not included with these bits but the AJAX one is  on CodePlex.

That’s two of my four demos from my Astoria talk. I’m currently trying to figure out how to retrofit the old ajax data services library into a new application.

http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=13357

There are probably lots more goodies in there, but these notes represent the pain I had to go through to get my Astoria demos that I’m doing this week at DevTeach working again.

Notes from updating some Entity Framework apps to the SP1 Beta bits

Along with Soma and others, the ADO.NET team has announced that the next beta of Entity Framework is available with the VS2008 Service Pack 1 BETA bits. The Astoria team already has a list of changes on their blog and there will be one coming for EF as well.

In the meantime….

Never a dull moment. I have two EF sessions this week at DevTeach and have to very quikly overhaul my demos.

Here are some of my notes.

EDMX Changes

Designer section

<edmx:ReverseEngineer /> has to be removed. I guess it was hopeful thinking.

SSDL

New attribute required inside of schema (Provider=”System.Data.SqlClient”)
These attributes had to be removed: DateTimeKind=”Unspecified” PreserveSeconds=”true”
ProviderManifestToken Attribute in schema declaration needs to be changed from the version number (eg for SS2005 that was ProviderManifestToken=.09.00.3054 now is ProviderManifestToken=2005.

Designer section is now at the bottom of the file. Nice.

You cannot have both the Design view and the XML view of an EDMX open at the same time.

In the Model Browser window, the XML for various pieces of the model are no longer shown in the tool tips. While there are reasons behind this, I REALLY liked this and am sad to see it go. Now I have to always close the designer and open up the XML and trudge through it if I just want to verify something. Oh well.

Designer Changes

Oh my its pretty! It’s cleaned up; the Asscoiation names are no longer clogging up the view, it’s more flexible wrt to what’s available on context menus and it has some nice shortcut icons for zooming etc.

Update from Model

As per a blog post from Noam Ben-Ami a while ago, this has improved enormously. With some minor excpetions (which I have not experienced) the CSDL stays intact when you update even though teh SSDL will be competely rewritten so any changes there will be lost. If you add new fields to a table in the database, and it’s obvious in the model where it should go, it will make it into the CSDL.

Code Generated Classes

Best bet: just delete them and let VS2008 regenerate them.

API Changes that I’ve experienced so far

It is very nice to have full graphs coming in and out of WCF services. 🙂

Believe it or not, I haven’t run into anything else so far that has impacted my apps except for the fact that I need to completely rearchitect my WCF solutions now that I have graphs to pass back and forth. Will I get this done before my session on using Entity Framework across tiers on Wednesday? Nothing like being forever behind the 8-ball. 🙂

The EntityDataSource control

I am not a big fan of drag & drop, but I’m liking this control so far. Here are some of the things that are winning me over.

Easily use inherited types

Easily do eager loading with the IncludePaths property.
Note: There’s a bug which is preventing the combination of eager loading when you are using an inherited types. That will go away with the next available build.

Many opportunities to override behavior. There are lots of events and the most important to me (so far) is the one lets me control what context the control will be tied to. By default, every EntityDataSource spins up its own context. That’s not good if you want to have relationship management happening.

Dynamically build queries with the properties or just entier an EntitySQL string as the command text.

Note that the where parameter takes an Entity SQL predicate, just like you would use with Query Builder methods.

That’s all for now.

Have fun!

David Yack at Vermont.NET tonight

Colorado man, author and MS Regional Directory, David Yack, is in Vermont to present at the VTdotNET meeting tonight, thanks to INETA.

He’ll be speaking on a topic we have NEVER had in our 6 years at the user group: VSTO. THen off to NJ tomorrow to talk about Silverlight 2.0.

But if that’s not diverse enough, he’s locked in his hotel room wrapping up a book on CRM. Go David! I’ve conspired with his wife, Julie, to be sure that he does get downtown to go buy some Lake Champlain Chocolates for his wife. We Julie’s know how to stick together!

Special thanks to Infragistics and JetBrains for their monthly raffles and to Goodrich (yes, that Goodrich) Vergennes for sponsoring the cost of the meeting space.

Nathan Myhrvold, Microsoft Research founder, in The New Yorker

This week’s The New Yorker issue is focused on invention and there is a really fun read on Nathan Myhrvold, Intellectual Ventures, dinosaurs, Alexander Graham Bell and more called In the Air.

Having been on the nosy Lake Washington cruise which goes by his house, Gates’ and others, it was funny to read his wife describe their house as “the place in the sci-fi movie where the aliens live”.

The article is focused on the process of invention and highlights some of the amazing brainstorming sessions that Myhrvold has with IV participants (including Bill Gates) who’s goal is to patent 1000 new inventions a year. They are at about 500 /year now, dealing with everyting from medicine to nuclear fision.

Entity Framework’s Golden Rule and its Platinum Rule

I have found myself explaining this in conference sessions so I thought I would write it down in my blog.

Entity Framework has a Golden Rule. Though shalt not do nuttin’ that the developer has not explicitly told you to do. The lack of implicit lazy loading is one of the more notable (and hotly contested by some) example of this.

But one day when I was coding, Entity Framework did something that I did not ask it to do. I emailed Danny Simmons to tattle on the API. But Danny explained something to me about RelationshipSpan that needs to override the Golden Rule.

I now call it the Platinum Rule. (I learned this pecking order from American Express).

The Platinum Rule is that an object graph (for example when a customer is attached to an order which is attached to some line items) must be completely in or completely outside of the ObjectContext.

The way I discovered it was that I had a Customer that was being change tracked by an ObjectContext.

I created a new order and attached the order to the customer.

But I did NOT add or attach the order to the context.

Yet, the order was suddenly within the context and being change tracked. Why? Because of the Platinum rule. By attaching the order to the customer, I was creating a graph. Since the graph can’t have some of its objects in the context and some of its objects out of the context, it has three options.

  1. Don’t allow the objects to be connected. (Now wouldn’t that tick you off?)
  2. Pull the customer out of the context. (Now wouldn’t that really tick you off?)
  3. Pull the order INTO the context.

Okay, works for me.

So this is the same rule that confuses people when they have an object graph that is IN the context and they detach one of its objects. That object leaves the context and “broken”off of the graph. So if you called context.Detach(Customer), then the customer is detached but the Order and its Line Items are still in the context.

You can no longer traverse from the Customer to the Order or from the Order to the Customer.

On the flip side, if you have an object graph that is not being change tracked  – the whole package, Customer, the order and the details and they are all attached to one another, if you attach any one of them to the context, the whole kit n’ caboodle gets pulled into the context – because of the platinum rule.

Another Domain Developer’s view of Entity Framework and Lazy Loading

Like Jeremy Miller, who has Transparent Lazy Loading for Entity Framework on his Christmas Wish List, Timothy Khouri does not like the fact that Entity Framework doesn’t support Lazy Loading. However, since he really likes Entity Framework, he wrote some code that will enforce Lazy Loading for him. Entity Framework promises not to do anything you don’t explicitly tell it to do, so Timothy found a way to keep telling EF to do this for him because that’s what he wants.

After looking at LINQ to SQL’s Lazy Loading and then bumping into Entity Framework’s explicit Deferred Loading, then sorting things out so he understood not only how the two are different but why, he shares his lesson and his workaround in this article: Entity Framework and Lazy Loading

Delete Stored Procs and Navigations in the Entity Data Model

Entity Data Models have a lot of rules. They are necessary for the integrity of the model, though if you are not familiar with depth of EDMs and *why* these rules exist, many of the mapping rules may seem to make no sense when it comes trying to implement them. This gets hairy when you are mapping stored procedures. Roger Jenning’s solution is to create stored procedures that will map properly, rather than suffer wtih the ones that already exist.

The first level of using Stored Procedures is that they work very easily for existing entities that are simple, have no EntityReferences (these involve Foreign Keys) , don’t involve inheritance and map directly to a single table in your database.

The next is stored procs that map nicely to entities which don’t involve inheritance but if your entities have any relationships, you will probably run into a wall with a delete procedure. Here’s why.

Stored Procs become Functions in the Entity Data Model. If you want to use these functions rather than the dynamic SQL that Entity Framework will create, it’s an all or nothing scenario. You need to map an Insert, and Update and a Delete. (Read procs doesn’t come into play here). There are plenty of blog posts and other resources on how to do this including this tutorial I wrote for the DataDeveloper.net site.

If you have a relationship in your entity, you can map that easily enough to a foreign key parameter in the function mappings and this makes sense with Inserts and Updates. With deletes, generally you only have a primary key (eg ContactID) as the parameter for the stored procedure. But here is where the EDM rules come in to play. A foreign key value that is used in a stored proc, would in most cases map to the EntityKey of a navigation property.

In other words, if you have a stored proc to insert SalesOrders, that procedure will have a parameter for CustomerID. The Order Entity this is mapping to has a relationship to the Customer entity and contains a Customer navigation property. The mapping therefore would link the CustomerID paraemter to Order.Customer.CustomerID.

By doing this, you are involving the association that exists between the Customer entity and the Order entity.

The EDM rule is that if you are involving an association in ONE of the function mappings, you have to use it in ALL o the function mappings.

Therefore a stored proc to delete an order, which only has an OrderID parameter will need also to have a CustomerID parameter because you are required to map SOMETHING to the Customer entity.

This then breaks a possible rule. It means you need to go to the dba and beg them to add this parameter into the stored procedure even though it doesn’t get used.

The alternative is to just go in and modify the SSDL to trick it into thinking that that parameter exists which will make the model happy and the database won’t care at all. IT doesn’t ever really get used.

So if the actual stored proc is

PROCEDURE DeleteOrder

@OrderID int

AS

DELETE from ORDERS WHERE OrderID=@OrderID

and the original function in SSDL is

 <Function Name=”DeleteOrder” Aggregate=”false” BuiltIn=”false” NiladicFunction=”false”
            IsComposable=”false” ParameterTypeSemantics=”AllowImplicitConversion” Schema=”dbo”>
     <Parameter Name=”OrderID” Type=”int” Mode=”In” />
 </Function>

You just need to add a new parameter to the SSDL

 <Function Name=”DeleteOrder” Aggregate=”false” BuiltIn=”false” NiladicFunction=”false”
            IsComposable=”false” ParameterTypeSemantics=”AllowImplicitConversion” Schema=”dbo”>
     <Parameter Name=”OrderID” Type=”int” Mode=”In” />
     <Parameter Name=”CustomerID” Type=”int” Mode=”In” />
 </Function>

This is all fine and good until you need to update the model, because the SSDL will lose all customizations.

I keep my customizations listed in a separate text file so that if I must update the model,  I can replace them.

It’s not pretty, but when I don’t have a lot of customizations, I will choose this over bugging the dba. Okay, in my world, I’m the DBA (how pathetic is that) but I’m trying to think about enterprise developers and real DBAs.

I started out wanting to write about dealing with stored procs for entities that are inherited, but I guess that will have to come later!