0

I have 2 tables that has a 1-1 relationship I am attempting to use the .Include() for LINQ to try to pass a child table entity to a property so that I can bind a child table field to a grid, along with fields from the parent table. Originally it works fine like this and I'm able to bind to the grid but only get results from the BUGroupBuildings table. I also need to bind to a field in the vwBuisnessUnits table.

 public IQueryable<BUGroupBuilding> GetBusinessUnitsBasedOnGroupID(int i)
    {
        var result = from d in this.ObjectContext.BUGroupBuildings
                     join b in this.ObjectContext.vwBusinessUnits on d.BU equals b.BU
                     where d.BUGroupID == i
                     orderby d.BU ascending
                     select d;

        return result;

    }

When I switch over to use the Include to bring back child table fields, I get an error

  public IQueryable<BUGroupBuilding> GetBusinessUnitsBasedOnGroupID(int i)
    {
        var result = from d in this.ObjectContext.BUGroupBuildings
                     .Include("vwBuisnessUnits")
                     select d;
        result = result.Where(w => w.BUGroupID == i).OrderBy(o => o.vwBusinessUnit.BU);

        return result;

    }

ERROR:

Load operation failed for query 'GetBusinessUnitsBasedOnGroupID'. A specified Include path is not valid. The EntityType 'EQUITYDWModel.BUGroupBuilding' does not declare a navigation property with the name 'vwBuisnessUnits'

Here's my entity enter image description here

I've added the necessary [Include] in the metadata.

    [MetadataTypeAttribute(typeof(BUGroupBuilding.BUGroupBuildingMetadata))]
public partial class BUGroupBuilding
{

    internal sealed class BUGroupBuildingMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private BUGroupBuildingMetadata()
        {
        }

        public string BU { get; set; }
        [Include]
        public BUGroup BUGroup { get; set; }

        public int BUGroupBuildingsID { get; set; }

        public Nullable<int> BUGroupID { get; set; }
        [Include]
        public vwBusinessUnit vwBusinessUnit { get; set; }

    }
}
Community
  • 1
  • 1
stevenjmyu
  • 926
  • 4
  • 16
  • 31
  • 1
    Why do you use `.Include("vwBuisnessUnits")` if you navigation property is vwBusinessUnit. Misspell? – Zabavsky May 23 '12 at 08:14
  • vwBusinessUnits is my child table name. Am I suppose to use the navigation name in the Inlcude? – stevenjmyu May 23 '12 at 14:18
  • I updated to Include("vwBusinessUnit") but got Load operation failed for query 'GetBusinessUnitsBasedOnGroupID'. A specified Include path is not valid. The EntityType 'EQUITYDWModel.BUGroupBuilding' does not declare a navigation property with the name 'vwBuisnessUnit' – stevenjmyu May 23 '12 at 16:11
  • There is a typo in your Include(), vwBuisnessUnit should be vwBusinessUnit. – JBrooks Sep 26 '13 at 20:06

2 Answers2

1

Did you generate the EntityModel from db or create it manually? Are you create a metada class manually?

Maybe some are wrong. Should be create a navigation property on client side (Web.g.cs file) like this:

/// <summary>
        /// Gets or sets the associated <see cref="tblCustomer"/> entity.
        /// </summary>
        [Association("tblCustomer_tblInvoice", "uiCustomerId", "Id", IsForeignKey=true)]
        [XmlIgnore()]
        public tblCustomer tblCustomer
        {
            get
            {
                if ((this._tblCustomer == null))
                {
                    this._tblCustomer = new EntityRef<tblCustomer>(this, "tblCustomer", this.FiltertblCustomer);
                }
                return this._tblCustomer.Entity;
            }
            set
            {
                tblCustomer previous = this.tblCustomer;
                if ((previous != value))
                {
                    this.ValidateProperty("tblCustomer", value);
                    if ((previous != null))
                    {
                        this._tblCustomer.Entity = null;
                        previous.tblInvoices.Remove(this);
                    }
                    if ((value != null))
                    {
                        this.uiCustomerId = value.Id;
                    }
                    else
                    {
                        this.uiCustomerId = default(Guid);
                    }
                    this._tblCustomer.Entity = value;
                    if ((value != null))
                    {
                        value.tblInvoices.Add(this);
                    }
                    this.RaisePropertyChanged("tblCustomer");
                }
            }
        }

Please check the Entity Model and a relationships.

1

Ahhh. I found the solution to my problem. Thought I would share it with the community. It turns out that I have to do a join and an Inlcude in the linq query when I have a custom relationship between 2 entities (in my case, a view and a table). When a relationship is defined in the model, it doesn't require an explicit join.

  public IQueryable<BUGroupBuilding> GetBusinessUnitsBasedOnGroupID(int i)
    {
        var result = from d in this.ObjectContext.BUGroupBuildings
                     join b in this.ObjectContext.vwBusinessUnits on d.BU equals b.BU
                     where d.BUGroupID == i
                     orderby d.BU ascending
                     select d;


        var r2 = from d2 in ((ObjectQuery<BUGroupBuilding>)result)
                 .Include("vwBusinessUnit")
                 select d2;

        return r2;            
    }
stevenjmyu
  • 926
  • 4
  • 16
  • 31