0

1 対 1 の関係を持つ 2 つのテーブルがあります。LINQ で .Include() を使用して、子テーブル エンティティをプロパティに渡そうとしています。これにより、子テーブル フィールドをフィールドと共にグリッドにバインドできます。親テーブルから。もともとはこのようにうまく動作し、グリッドにバインドできますが、BUGroupBuildings テーブルからしか結果を取得できません。また、vwBuisnessUnits テーブルのフィールドにバインドする必要があります。

 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;

    }

Includeを使用して子テーブル フィールドを戻すように切り替えると、エラーが発生します。

  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;

    }

エラー:

クエリ 'GetBusinessUnitsBasedOnGroupID' の読み込み操作が失敗しました。指定されたインクルード パスが無効です。EntityType 'EQUITYDWModel.BUGroupBuilding' は、'vwBuisnessUnits' という名前のナビゲーション プロパティを宣言していません

これが私のエンティティです ここに画像の説明を入力

必要な [Include] をメタデータに追加しました。

    [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; }

    }
}
4

2 に答える 2

1

EntityModel を db から生成したか、手動で作成しましたか? メタデータ クラスを手動で作成していますか?

多分いくつかは間違っています。次のように、クライアント側 (Web.g.cs ファイル) でナビゲーション プロパティを作成する必要があります。

/// <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");
                }
            }
        }

エンティティ モデルと関係を確認してください。

于 2012-05-23T07:53:36.797 に答える
1

ああ。私の問題の解決策を見つけました。コミュニティと共有しようと思いました。2 つのエンティティ (私の場合はビューとテーブル) の間にカスタムの関係がある場合、linq クエリで結合と Inlcude を実行する必要があることがわかりました。モデルでリレーションシップが定義されている場合、明示的な結合は必要ありません。

  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;            
    }
于 2012-05-25T17:26:34.803 に答える