Daily Archives: January 22, 2008

Am I a web site or a web app project: CodeFile vs. CodeBehind

I accidentally opened up a web application project as a website. That’s not too hard to do. Just say you want to open a web site and point to the folder. It had been a while and I forgot that it was a WAP.

So I’m working on a web page and double click on a control and find myself in the markup with a new script tag and the .net code for the click event of the control. That wasn’t good considering I already had some nice event handlers for that control in my code behind.

I spent a lot of time going back and forth trying to figure out why I couldn’t get to my code behind.

I even remembered having this problem in the early days of Web App Project and the solution was to “convert to a Web Application Project” from the context menu in the solution explorer. But this option wasn’t available.

I created a new web site and looked at the page directive and noticed that in the new website the parameter pointing to the code behind file was called CodeFile, but in my old site it was CodeBehind. Wierd.

So with that clue I googled those two words, came across Steve Smith’s blog post about the opposite problem and realized my mistake!

Duh. 🙂

Danny Simmons is providing guidance for dealing with EF and Web Services

It seems to me that Danny Simmons must be working on v2 of Entity Framework.

The posts he has written in the past week are examples of things that would probably have been in V1 had they had the time and resources. Now they are winding down v1, building up the team and thinking about v2.

Danny has written three blogs posts filled with really handy extension methods for entity framework (and LINQ to Objects) [here’s the third which contains pointers to the first two] and now he is building a state bag for dealing with getting  entities across boundaries SOA style (i.e. web services and dealing with the limitations of XML serialization – something many of us have been writing about for a while).

I”m really pleased to see this coming out of Microsoft (although what happened to whitepapers? ;-)). I have come up with my own methods (some with help from Danny and some on my own) but it’s always a little unnerving when you are just making your best guess at what they might have planned for.

Months ago I started a thread on the MSDN forum about this issue and it ended up being a 3 or 4 page thread because every time someone on the team said “well, just do this” I said “no – that doesn’t really solve the problem”. I was pretty relentless (thanks to all of the work I have done with web services and ws-* in the past) and finally Danny ended the thread with “okay, obviously there’s more we need to look at here.”

Since then I have struggled to come up with disconnected solutions that deal with binary and xml serialization and presented them at a few conferences. This was with Beta2 and there are some new goodies in Beta3 that will ease the pain somewhat but it’s still pretty complex.

For the last week or so, Danny has been writing a series of blogs where he’s using his indepth understanding of entities, object services, statemanagment, etc. to build a “state bag” for transferring the whole kit ‘n caboodle across processes. It’s impressive as hell but there was something bugging me that I was dwelling on, and Roger Jenning’s pointed it out on his blog as well.The answer to “why is ObjectContext not serializable?” is that it’s big and it would not be scalable. And no, I’m not suggesting that Danny forgot this. 🙂 Which is why I’m biding my time to see how it comes out.

I’m assuming that Danny’s state bag is a proof of concept for a v2 solution (though he has been clear that because of some obvious issues with interop, it just may not go in.) And as he suggested in today’s post, readers (like me) are probably “chomping at the bit” to see how he does the object context part so that we can see how streamlined it is.

Most people have no clue how deep Entity Framework goes and what the ADO.NET team is setting up for us to be able to do with it down the road. It’s probably clear by now that I’m really excited about Entity Framework and I’m struggling to keep up with all of the great ideas that people are trying out as evidenced in the forums.

So while we’re all looking forward to the release of Entity Framework, I’m already starting to get excited about what’s going to come in V2.

Thinking about the EntityDataSource

I’ve been thinking about the EntityDataSource that has been mentioned in the forums. It is something that is going to be added to Entity Framework by RTM, but it doesn’t exist in current bits for us to experiment with.

The reason I’ve been thinking about it is because of some of the issues/confusion with the LINQDataSource and I’m really hoping that the EF team will be looking at that, learning from it, and making sure that some of those problems don’t exist in the new component.

Most of the issues with the LINQDataSource are a result of very realistic limitations. But where I see the problem is just the difficulty many users have using it because they don’t understand what’s going on underneath and to work within the limitations, there are some very non-intuitive things they need to do.

I wrote a bunch of LINQDataSource demos for a presentation and spent a lot of time learning workarounds in order to achieve some very common scenarios although not quite as common as the typical demos. That’s where I hope EntityDataSource makes it easier. I don’t want to have to work so hard to discover how to make it work.

Here’s an example of a something I think is a common scenario.

I have an Entity for SalesOrderHeaders from AdventureWorksLT.

I would like to have an editable gridview of these records.

I’d like to display the customer name in the grid not the customer ID.

Not too strange, right?

But there are a lot of issues encountered along the path to create this solution.

1)One of the most often asked questions about the LINQDataSource is “why can’t I edit?”. The answer is most often because the developer selected the fields they wanted displayed in the grid and a huge number of people don’t realize that they are creating an anonymous type by doing this. Yes there are some clues. The “advanced’ button goes away. There’s no “enable delete” etc. checkboxes on eh Task menu.

A better way? It would be much easier if the wizard started out by asking if you have any intention of editing and if the answer was yes, does NOT present the end user with the option to select fields.

An even better way? I know the model won’t allow this but it would be great to have another entity definition (subset of the properties) for this entity without creating an EntityKey conflict.

2) This also means that I can’t really take touch the CustomerName in the EntityRef (SalesOrderHeaders.Customer)unless I create an anonymous type, which I don’t want to do. So the next obvious solution is to create a LINQDataSource for the Customers and format the CustomerID column in my gridview to be a template field with a dependency on the CustomerLINQDataSource.

         <asp:templatefield HeaderText=”CustomerID” SortExpression=”CustomerID”>
                    <edititemtemplate>
                        <asp:DropDownList ID=”DropDownList1″ runat=”server”
                            DataSourceID=”LinqDataSource2″ DataTextField=”CompanyName”
                            DataValueField=”CustomerID” SelectedValue='<%# Bind(“CustomerID”) %>’>
                        </asp:DropDownList>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:Label ID=”Label1″ runat=”server” Text='<%# Eval(“Customer.CompanyName”) %>’></asp:Label>
                    </itemtemplate>
                </asp:templatefield>

But this creates yet another problem that I may not have any awareness of. The default deferred loading will cause an extra round trip to the database for each SalesOrderHeader record to get the CustomerName.

A better way? Give us a Deferred Loading or Eager Loading option in the wizard!

3) There is a really weird gotcha because we are using EntityRefs for the CustomerID/CustomerName DropDown. Editing doesn’t work properly unless you set the LINQDataSource property to StoreOriginalValuesinViewState to False. It’s logical but again, totally non-intuitive. If you don’t do it, you will get this error message:
“System.InvalidOperationException: Could not find a row that matches the given keys in the original values stored in ViewState.  Ensure that the ‘keys’ dictionary contains unique key values that correspond to a row returned from the previous Select operation.” That’s scary, huh? It took some googling to figure this one out.

A better way? If the LINQDataSource could trap this exception and give some better guidance about what the problem is and how to deal with it. Or something…


These are some of the things I spent a ridiculous amount of time on in order to get this scenario working. And seems like nothing compared to what Roger Jennings banged his head on!

It would be great not to have to go through this again with the ObjectDataSource, if it’s possible to make some of these issues easier to deal with!