Monday, July 12, 2010

Be careful with CreateTablesFromModel

I started with the Windows Azure by following an online course of UK Dev. This course was a compilation of reading white papers, following recorded sessions and the Windows Azure Labs. While working with these labs, I wanted to start from scratch instead of using the start solution.

So I started to set up the N-tier solution and added all the necessary references. I created the whole solution as described in the lab, but I used my own naming convention. When everything was set-up I pressed the F5 button and the application started. So far, so good. The web form started, I filled in my web form and pressed the save button to persist my data… and received a DataServiceRequestException.

In the Inner Exception I was able to discover a little more information:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="">
<message xml:lang="nl-BE">The table specified does not exist.</message>

At this stage I was surprised. The CreateTablesFromModel method didn’t give me any notice that something went wrong while creating my model. So I went looking in the development storage database what was missing. In the TableRow table I discovered that a row was added, but the name in the column ‘TableName’ wasn’t the name that I was expecting. I named my domain class ‘Entry’ but in the row that was added, the table name was ‘Entries’.

So I went looking in my code for the name ‘Entries’ to find out that I had a property called ‘Entries’ in the implementation of my own DataContext.

/// <summary>
/// Exposes the Entry table
/// </summary>
public IQueryable<Entry> Entries
return this.CreateQuery<Entry>("Entry");

I presumed that my tables would be created by the name given to the class that persists the data. However, the names of the properties in the DataContext are used to create the table names. At this point I had 2 possibilities:
· I rename the property in the DataContext to ‘Entry’
· I change the string parameter in the CreateQuery method inside the property to ‘Entries’
The last option meant that I would have to look up where else this parameter is used, so I can change it to ‘Entries’. (The AddObject method was one of the methods I had to change)

No comments:

Post a Comment