Tip 49 – How to find your Data Service bug

I’ve been playing around with creating a custom Astoria Data Service Provider, aka DSP, for a while now. So far I’d managed to get my metadata all setup, so browsing to $metadata works great. I’d got a few simple queries work. Basically everything was going fine. At least until I tried this URL: I wasn’t…

4

Tip 48 – How to host a Data Service in WCF

Every wonder if you can host an Astoria Data Service in WCF? Well turns out the answer is yes, in fact once you’ve got your references set up etc it is pretty easy. Step 1 – Setting up your project Here is what my working project looks like: In this example I am using VS…

1

Tip 47 – How fix-up can make it hard to change relationships

Problem: Take this code: Category oldCategory = ctx.Categories     .Include(“Products”)     .First(c => c.Name == “Drink”); Category newCategory = new Category {Name = “Beverage”}; foreach(Product product in oldCategory.Products) {     newCategory.Products.Add(product); } Ignore for a second that in this example the solution is probably just to rename the oldCategory “Beverages” – Coming up with real…

2

Tip 46 – How to exclude a property using Code-Only

This time a real simple one prompted by this question on StackOverflow. Problem: If you tell the Entity Framework about this class using Code-Only, by default every property becomes part of Entity, and as a result stored in the database. Usually this is what you want. But not always, imagine this class: public class Person{…

1

Tip 45 – How to swap EF metadata at runtime.

Background By default the Entity Framework embeds its metadata inside your assembly as a resource. It also puts a connection string in the App or Web Config that references those resources something like this: <add name=”BloggingEntities” connectionString=”metadata=res://*/Blogging.csdl|res://*/Blogging.ssdl|res://*/Blogging.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;Initial Catalog=TipsDatabase;Integrated Security=True;MultipleActiveResultSets=True&quot;” providerName=”System.Data.EntityClient” /> This makes it incredibly easy to get started: using (BloggingEntities ctx…

3

Tip 44 – How to navigate an OData compliant service

I recently did a crash course in Data Services and OData. While doing so I realized my notes might be useful for you guys. So here is my little cheat sheet to quickly get up to speed with OData Urls. Note: OData Services may not necessarily support all of the following features: but if they…

7

Tip 43 – How to authenticate against a Data Service

Problem: When writing code against a Data Service, like say SharePoint, the client application must provide a valid set of credentials, or you will see a dreaded “401 Unathorized” response. For Silverlight applications hosted on the same site as the DataService, this is generally handled for you automatically. But WPF applications, for example, need manual intervension. Solution: The…

0

Interesting series of Posts exploring Code-Only

Daniel Wertheim left a couple of comments on Tip 42, that point to a series of posts he has written about using Code-Only to write clean code. The series is really interesting as he delves into topics like: facades, relationships between non-public properties, integrating pluralization, using structure map etc. All well worth a read, so…

1

Tip 42 – How to create a dynamic model using Code-Only

Background: When we give examples of how to use Code-Only we always start with a strongly typed Context derived from ObjectContext. This class is used to bootstrap the model. For example this (property bodies omitted for simplicity sake): public class MyContext : ObjectContext {     public ObjectSet<Category> Categories { get; }    public ObjectSet<Product> Products…

5

Tip 41 – How to execute T-SQL directly against the database

Sometimes you’ll find you need to issue a query or command that the Entity Framework can’t support. In fact this problem is common to most ORMs, which is why so many of them have a backdoor to the database. The Entity Framework has a backdoor too… .NET 3.5 SP1 In .NET 3.5 SP1 you can…

4