Posts tagged with "instructional"

Inline Count in the OData Spec

When it comes time to tackle a new (to you) technology, how do you begin? Do you read others’ experiences from blogs? Wait for screencast instructions from a site like Pluralsight?

There are a lot of ways to learn, but I’m reminded of the value of just going back to the spec. Sometimes the author of the blog article you’re reading is in your same boat trying to learn, and going directly to the spec, you might get to bypass a lot of wasted trial and error cycles.

I’ve spent a lot of time in the HTML5 spec the last while, and somewhat recently also the OData spec (which you can find at odata.org). I’ve read a lot of blogs and seen a lot of videos on the subject of OData, but when I finally found my way to the actual spec, I was pleased to find a very concise and obviously thorough coverage of the topic.

One of the little things I learned which has big implications for me is the ability to specify a query option called $inlinecount. With this query option specified, an OData query will bring back a count of all entities in the queried collection (applied after any filters) even if some limiting options such as paging or $top are included.

Take, for example, the following query…

http://services.odata.org/OData/OData.svc/Products?$inlinecount=allpages&$top=10&$filter=Price gt 200

The query should find the first 10 products whose price is greater than 200. If you’re retrieving these products to be displayed in a web page, however, and you need to worry about paging and you need to tell the pager how many pages to render, you’re going to need to know the **total **number of products that with a price greater than 200 even though this query only asks for the top 10 (don’t use $top for your paging, BTW, there’s a better way). The inclusion of inlinecount, in this query dictates the inclusion of the following element in the response…

<m:count>24</m:count>

With this additional information about the submitted query’s results, your pager now knows to render 3 pages (of 10).

Notice that the Price gt 200 filter did get applied before the result for inlinecount was calculated, so we did not receive back an inline count of all product entities.

This is certainly just a shallow glance at the topic. If you want more in-depth information about OData or about the inlinecount query option, you should go straight to the spec :)

Troubleshooting Your EF Code First Mapping

Here’s the scenario. You’re a developer and a you create a great set of objects and relationships between them that perfectly expresses the structure of your prospective application. You want to just tell your code to go ahead and create a new database (or map to an existing one) that matches your model and allows you to store the data?

So you do this with EFCF and all is well. Except that occasionally you tweak your model and run into a mapping error and all is not well. EF has a hard time communicating to you what exactly is wrong and can give some pretty obscure exception messages, and you can spend a lot of time (personal experience) tracking down what you’ve done wrong.

The error I just ran into is this one: “Type Address_Country is not defined in namespace PEP.CIM.Entities.Mapping (Alias=Self).” Huh? I know that I just tried to create a Country navigation property on my Address entity so I have a general idea of where my error might be, but after a double check of my code I can’t see how it’s any different from my other relationships that are working fine. So what’s the problem?

I stumbled upon a great way to troubleshoot these kinds of issues, though… the Entity Framework Power Tools (at CTP1 as I type). I have used this tool countless times to reverse engineer a database into good-lookin’ C# code, but today I used it for something else. When the tool is installed (and you have EF referenced in your project!), you can right click on your context and get a few options under the Entity Framework menu item.

In my case, I was able to use View Entity Data Model XML and then look through the resulting XML for the Address_Country relationship. I fairly quickly discovered that I had NavigationProperty elements for my Address_Country but I didn’t have a coresponding Association (as I did for my working relationships). This lead me back to my fluent mapping statement…

HasRequired(t => t.Country).WithMany(t => t.AddressesByAdrCountryCdvId).HasForeignKey(t => t.AdrStateProvinceCdvId); HasRequired(t => t.State).WithMany(t => t.AddressesByAdrCountryCdvId).HasForeignKey(t => t.AdrStateProvinceCdvId);

where I discovered that I had made a grievous copy paste error. As you can see, I’m attempting to add mappings for Country and State, but it’s all wrong. Each is mapping AddressesByAdrCountryCdvId and AdrStateProvinceCdvId. Here’s the way it should look…

HasRequired(t => t.Country).WithMany(t => t.AddressesByAdrCountryCdvId).HasForeignKey(t => t.AdrCountryCdvId); HasRequired(t => t.State).WithMany(t => t.AddressesByStateProvinceCdvId).HasForeignKey(t => t.AdrStateProvinceCdvId);

Now that’s better! Now not only does my build succeed, but my unit tests all pass as well.

It also may help to use the same technique to View Entity Data Model DDL SQL and see what SQL would be generated to create your tables and constraints.

I hope this helps you find mapping issues in your code first projects. Happy coding.