1

I have a model that is defined in EF database first edmx. From there I expose some tables and views (mainly views). As it's possible to augment the EF model with OData, how could I add a navigation property of a complex type to another EF and OData exposed type?

Currently I define a partial class and add the properties and attributes using them. But it looks like it's possible to add the desired properties with OData's modelbuilder functionality too, or perhaps better yet, first use ODataConventionModelBuilder and then augment the results. Alas, I'm unable to stitch together a working example from the existing API documentation and examples I've found.

Here's the code

//This class is generated from a view by EF (edmx)...
public partial class AccountView
{
    public System.Guid Id { get; set; }
    public int CompanyId { get; set; }
}

//Here's augmenting the EF generated view with some additional data...
[MetadataType(typeof(AccounViewMetaData))]
public partial class AccounView
{  
    //This is added here explicitly. AccountView itself exposes just
    //a naked key, CompanyId.
    public virtual Company Company { get; set; }

    //This is just in case...
    public class AccounViewDomainMetaData
    {
        //This is to add a navigation property to the OData $metadata. How to do this
        //in WebApiConfig? See as follows...
        [ForeignKey("Company")]
        public int CompanyId { get; set; }
    }
}

//This is an EF generated class one from an edmx..-
public partial class Company
{
    public Company() { }
    public int CompanyID { get; set; }
    public string Name { get; set; }
}

//How to add a navigation property from AccountView to Company so that it'd become
//possible to call http://example.com/Accounts?$expand=Company and http://example.com/Accounts(1)?$expand=Company ?
var builder = new ODataConventionModelBuilder();
var companySet = builder.EntitySet<Entities.Company>("Companies");
var accountSet = builder.EntitySet<Entities.AccountView>("Accounts");
accountSet.EntityType.HasKey(i => i.Id); //EF has hard time recognizing primary keys on database first views...
//How to hide this from the result if there's a way to create a ?$expand=Company navigation property?
//accountSet.EntityType.Ignore(i => i.CompanyId);

This is related to my other question regarding OData and models.

Community
  • 1
  • 1
Veksi
  • 3,556
  • 3
  • 30
  • 69
  • 1. OData Protocol do allow add the navigation property to a complex type, but ODL library and WebAPI does not support navigation in complex type now. 2. I am not sure if a understand right. Do you want to add some additional navigation property in to the EF generated model. Does the data of navigation property will be saved in DB? or just handle in WebAPI layer? – Maya Dec 24 '14 at 07:17
  • First, sorry for the reply! I succeeded adding ``=expand``, which is what I tried to do (my OData jargon isn't up to the notch, so to speak). And no, there were no intention to add the navigation properties to the database, but handle in code. In any event, the situation passed, the code can be seen at [How does one define an (optional) navigation property (?$expand=) in code?](http://stackoverflow.com/questions/27574152/how-does-one-define-an-optional-navigation-property-expand-in-code). – Veksi Jan 15 '15 at 11:40

0 Answers0