Search code examples
dictionaryentityuribreeze

How does breeze detemine API action method based on entity name?


I have a simple entity: Contact

DBContext line:

public DbSet<Contact> Contacts { get; set; }

API Controller:

[HttpGet]
public IQueryable<Contact> Contacts()
{
       return _contextProvider.Context.Contacts;
}

In my data retrieval in breeze client I do this:

var query = EntityQuery.from("Contacts")
            .orderBy(orderBy.obContact)

That works great, and I can understand that the from parameter "Contacts" must match the API action method.

In my getByID I do this:

return manager.fetchEntityByKey("Contact", contactId, true)

That's working great, and it also makes a call to the "Contacts" API method. But I would like to know how breeze took the parameter "Contact" and knew to call the "Contacts" method.

Is this in the metadata?

Did it come from my DBSet line in my dbcontext? (I'm thinking it did, but would like confirmation). If that's the case then these two names must be equal right?

[HttpGet]
public IQueryable<Contact>Contacts()    // The HTTP action method name

public DbSet<Contact> Contacts { get; set; }  // The DbSet variable

I tried these changes:

public DbSet<Contact> DBSetContacts { get; set; }

and

[HttpGet]
public IQueryable<Contact> Contacts()
{
    return _contextProvider.Context.DBSetContacts;
}

My first query above that returns an array ran fine. My fetch by ID failed, it was trying to find URI resource "DBSetContacts".

My conclusion is that the DbSet variable has to have the same name as the URI method for the fetchByID to work. Is that correct?


Solution

  • Breeze internally keeps an EntityType/ResourceName map. ResourceNames are the names of the server side methods.

    The EntityType/ResourceName map is one of the items in the Breeze MetadataStore. The map of a new MetadataStore starts empty. Breeze populates it from server metadata if those metadata contain EntityType/Resource mappings.

    As you guessed, the Breeze EFContextProvider generates metadata with mappings derived from Entity Framework DbSet names. When you define a Person class and exposed it from a DbContext as a DbSet named "Persons", the EFContextProvider metadata generator adds a mapping from the "Persons" resource name to the Person entity type.

    For more information see the 'EntityType/ResourceName" mapping subtopic on this page.

    http://www.getbreezenow.com/documentation/querying-locally

    You can also update the EntityType/ResourceMap explicitly via the method below:

    http://www.getbreezenow.com/sites/all/apidocs/classes/MetadataStore.html#method_setEntityTypeForResourceName