I am using Entity Framework models on a database and exposing it over WCF using OData.
I added a new table in my database linking 2 other tables to each other in a many to many relationship. The table does not have a primary key, just 2 columns which are foreign keys to each of the respective tables.
I re-generated my Entity models and the classes had navigation properties created:
public partial class rmApplication
{
public rmApplication()
{
this.rmDeploymentRequests = new HashSet<rmDeploymentRequest>();
this.rmEnvironments = new HashSet<rmEnvironment>();
}
public int ApplicationId { get; set; }
public string Name { get; set; }
public virtual ICollection<rmDeploymentRequest> rmDeploymentRequests { get; set; }
//This is the new navigation property
public virtual ICollection<rmEnvironment> rmEnvironments { get; set; }
}
I also regenerated my service reference and can see the navigation properties there too:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public global::System.Data.Services.Client.DataServiceCollection<rmEnvironment> rmEnvironments
{
get
{
return this._rmEnvironments;
}
set
{
this._rmEnvironments = value;
this.OnPropertyChanged("rmEnvironments");
}
}
The problem is that when I retrieve an rmApplication object over OData, and try to expand the navigation property like:
var app = context.rmApplications.Expand("rmEnvironments").Where(a => a.ApplicationId == applicationId).First();
it throws an exception saying:
Could not find a property named 'rmEnvironments' on type 'dbModel.rmApplication
In the .edmx I can see the associations exist:
<AssociationSet Name="FK__rmApplica__Appli__2DDD7BCE" Association="dbModel.Store.FK__rmApplica__Appli__2DDD7BCE">
<End Role="rmApplication" EntitySet="rmApplication" />
<End Role="rmApplicationEnvironments" EntitySet="rmApplicationEnvironments" />
</AssociationSet>
<AssociationSet Name="FK__rmApplica__Envir__2ED1A007" Association="dbModel.Store.FK__rmApplica__Envir__2ED1A007">
<End Role="rmEnvironment" EntitySet="rmEnvironment" />
<End Role="rmApplicationEnvironments" EntitySet="rmApplicationEnvironments" />
</AssociationSet>
and the names match up to the foreign key constraint names in my database.
What could be causing this not to work? When I retrieve an rmApplication object without the Expand it has the rmEnvironments property with count 0.