Question

I'm trying to retrieve a single entity by its ID in the DomainService.

However the type of that entity is not generated in the client. Here is my query:

public Contact GetContact(int contactId)
{
  return DbContext.Contacts
    .Include(c => c.Phones)
    .Include(c => c.Addresses)
    .SingleOrDefault(c => c.ContactId == contactId);
}

The Phone entity isn't generated on the client.

I tried decorating this method with [Query], but then I get the following compile-time exception:

Query operation 'GetContact' cannot be marked composable since its return type 'Models.Contact' does not implement IEnumerable of T.

Is there any way to create a WCF RIA query that returns a single entity by ID?

Was it helpful?

Solution

Setting the IsComposable property will allow it to build. But as in our discussion, I don't know why your Phone and Address entities are not being generated.

[Query(IsComposable=false)]
public Contact GetContact(int contactId)
{
  return DbContext.Contacts
    .Include(c => c.Phones)
    .Include(c => c.Addresses)
    .SingleOrDefault(c => c.ContactId == contactId);
}

OTHER TIPS

RIA Service methods require an IQueryable<T> or IEnumerable<T> to work. It works with change sets, not single elements.

Change it to this (and stop trying to return a single object):

public IQueryable<Contact> GetContact(int contactId)
{
  return DbContext.Contacts
    .Include(c => c.Phones)
    .Include(c => c.Addresses)
    .Where(c => c.ContactId == contactId);
}

Simply apply your FirstOrDefault() to the client side code (once the data is loaded of course).

RIA-enabled controls like DomainDataSource and DataPager use LINQ 'Take()' with just about every single load operation they generate (check your Fiddler trace to see what I mean). I haven't watched for long enough to say its 100% of the time, but its most of the time. These controls expect an IQueryable, and will barf at random otherwise when you try and do things like sorting, pagination, grouping, etc. It's really not that big a deal to call .FirstOrDefault().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top