1
votes

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.

1
I strongly suggest that all tables have primary keys, especially if you want to use EF. - Eric Scherrer
@EricScherrer Do you think this could be causing the problem? - mclaassen
You mean I have to read the whole question? Fine :) - Eric Scherrer
It might help, I do see you have the relationships defined which should give the hints EF needs to build the navigation properties. And the error message is complaining it can not find a property that you have confirmed is there. I'm stumped for now. - Eric Scherrer
You ever get this figured out? - Eric Scherrer

1 Answers

0
votes

As we troubleshooted in the comments, the issue was that the table needed a primary key for Ef to generate the appropriate navigational properties.