0

多対多リレーションシップを形成するデータベースに次の表があるとします。また、EF 4.1を使用したASP.Net MVCプロジェクトでは、対応するPOCOエンティティとデータベースにアクセスするリポジトリ層があります。EF 4.1、データベースの最初と多対多の関係 - すべてのサブオブジェクトを取得する方法?

People: PersonId PK, Firsname, Lastname 
FavoriteRestaurants: ID PK, PersonId, RestaurantId 
Restaurants: ResaurantId PK, Name 

私はPERSONIDを持っている場合は、このすべての人のお気に入りのレストランの名前をリストするための最良の方法は何ですか?

または、リポジトリレイヤを使用している人のIDに基づいてこのEnumerableを初期化するにはどうすればよいですか?

IEnumerable MyFavoriteRestaurants = ???

私は、Person.FavoriteRestaurantsコレクションをループするためにforeachを使用することができますが、FavoriteRestaurant.Restaurantを1つずつ取り出すことはできますが、コードの1行でこれを行うより洗練された方法があるかどうかは疑問でした。ここで

UPDATE

は私のプロジェクトで人POCOエンティティクラスの一例です。このコードは、ここからダウンロードマイクロソフトのテンプレートによって生成された:

http://visualstudiogallery.msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313/

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated from a template. 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Collections.Specialized; 

namespace UpDir.Domain 
{ 
    public partial class Person : EntityBase 
    { 
     #region Primitive Properties 

     public virtual int PersonId 
     { 
      get; 
      set; 
     } 

     public virtual string Firstname 
     { 
      get; 
      set; 
     } 

     public virtual string Middlename 
     { 
      get; 
      set; 
     } 

     public virtual string Lastname 
     { 
      get; 
      set; 
     } 

     public virtual string Nickname 
     { 
      get; 
      set; 
     } 

     public virtual string EncryptedEmail 
     { 
      get; 
      set; 
     } 

     public virtual string Photo 
     { 
      get; 
      set; 
     } 

     public virtual short StatusId 
     { 
      get { return _statusId; } 
      set 
      { 
       if (_statusId != value) 
       { 
        if (Status != null && Status.StatusId != value) 
        { 
         Status = null; 
        } 
        _statusId = value; 
       } 
      } 
     } 
     private short _statusId; 

     #endregion 
     #region Navigation Properties 

     public virtual ICollection<Member> Members 
     { 
      get 
      { 
       if (_members == null) 
       { 
        var newCollection = new FixupCollection<Member>(); 
        newCollection.CollectionChanged += FixupMembers; 
        _members = newCollection; 
       } 
       return _members; 
      } 
      set 
      { 
       if (!ReferenceEquals(_members, value)) 
       { 
        var previousValue = _members as FixupCollection<Member>; 
        if (previousValue != null) 
        { 
         previousValue.CollectionChanged -= FixupMembers; 
        } 
        _members = value; 
        var newValue = value as FixupCollection<Member>; 
        if (newValue != null) 
        { 
         newValue.CollectionChanged += FixupMembers; 
        } 
       } 
      } 
     } 
     private ICollection<Member> _members; 

     public virtual ICollection<Message> Messages 
     { 
      get 
      { 
       if (_messages == null) 
       { 
        var newCollection = new FixupCollection<Message>(); 
        newCollection.CollectionChanged += FixupMessages; 
        _messages = newCollection; 
       } 
       return _messages; 
      } 
      set 
      { 
       if (!ReferenceEquals(_messages, value)) 
       { 
        var previousValue = _messages as FixupCollection<Message>; 
        if (previousValue != null) 
        { 
         previousValue.CollectionChanged -= FixupMessages; 
        } 
        _messages = value; 
        var newValue = value as FixupCollection<Message>; 
        if (newValue != null) 
        { 
         newValue.CollectionChanged += FixupMessages; 
        } 
       } 
      } 
     } 
     private ICollection<Message> _messages; 

     public virtual ICollection<Notification> Notifications 
     { 
      get 
      { 
       if (_notifications == null) 
       { 
        var newCollection = new FixupCollection<Notification>(); 
        newCollection.CollectionChanged += FixupNotifications; 
        _notifications = newCollection; 
       } 
       return _notifications; 
      } 
      set 
      { 
       if (!ReferenceEquals(_notifications, value)) 
       { 
        var previousValue = _notifications as FixupCollection<Notification>; 
        if (previousValue != null) 
        { 
         previousValue.CollectionChanged -= FixupNotifications; 
        } 
        _notifications = value; 
        var newValue = value as FixupCollection<Notification>; 
        if (newValue != null) 
        { 
         newValue.CollectionChanged += FixupNotifications; 
        } 
       } 
      } 
     } 
     private ICollection<Notification> _notifications; 

     public virtual Status Status 
     { 
      get { return _status; } 
      set 
      { 
       if (!ReferenceEquals(_status, value)) 
       { 
        var previousValue = _status; 
        _status = value; 
        FixupStatus(previousValue); 
       } 
      } 
     } 
     private Status _status; 

     public virtual ICollection<UpDirEmail> FromEmails 
     { 
      get 
      { 
       if (_fromEmails == null) 
       { 
        var newCollection = new FixupCollection<UpDirEmail>(); 
        newCollection.CollectionChanged += FixupFromEmails; 
        _fromEmails = newCollection; 
       } 
       return _fromEmails; 
      } 
      set 
      { 
       if (!ReferenceEquals(_fromEmails, value)) 
       { 
        var previousValue = _fromEmails as FixupCollection<UpDirEmail>; 
        if (previousValue != null) 
        { 
         previousValue.CollectionChanged -= FixupFromEmails; 
        } 
        _fromEmails = value; 
        var newValue = value as FixupCollection<UpDirEmail>; 
        if (newValue != null) 
        { 
         newValue.CollectionChanged += FixupFromEmails; 
        } 
       } 
      } 
     } 
     private ICollection<UpDirEmail> _fromEmails; 

     public virtual ICollection<UpDirEmail> ToEmails 
     { 
      get 
      { 
       if (_toEmails == null) 
       { 
        var newCollection = new FixupCollection<UpDirEmail>(); 
        newCollection.CollectionChanged += FixupToEmails; 
        _toEmails = newCollection; 
       } 
       return _toEmails; 
      } 
      set 
      { 
       if (!ReferenceEquals(_toEmails, value)) 
       { 
        var previousValue = _toEmails as FixupCollection<UpDirEmail>; 
        if (previousValue != null) 
        { 
         previousValue.CollectionChanged -= FixupToEmails; 
        } 
        _toEmails = value; 
        var newValue = value as FixupCollection<UpDirEmail>; 
        if (newValue != null) 
        { 
         newValue.CollectionChanged += FixupToEmails; 
        } 
       } 
      } 
     } 
     private ICollection<UpDirEmail> _toEmails; 

     public virtual ICollection<UpDirEvent> UpDirEvents 
     { 
      get 
      { 
       if (_upDirEvents == null) 
       { 
        var newCollection = new FixupCollection<UpDirEvent>(); 
        newCollection.CollectionChanged += FixupUpDirEvents; 
        _upDirEvents = newCollection; 
       } 
       return _upDirEvents; 
      } 
      set 
      { 
       if (!ReferenceEquals(_upDirEvents, value)) 
       { 
        var previousValue = _upDirEvents as FixupCollection<UpDirEvent>; 
        if (previousValue != null) 
        { 
         previousValue.CollectionChanged -= FixupUpDirEvents; 
        } 
        _upDirEvents = value; 
        var newValue = value as FixupCollection<UpDirEvent>; 
        if (newValue != null) 
        { 
         newValue.CollectionChanged += FixupUpDirEvents; 
        } 
       } 
      } 
     } 
     private ICollection<UpDirEvent> _upDirEvents; 

     #endregion 
     #region Association Fixup 

     private void FixupStatus(Status previousValue) 
     { 
      if (previousValue != null && previousValue.People.Contains(this)) 
      { 
       previousValue.People.Remove(this); 
      } 

      if (Status != null) 
      { 
       if (!Status.People.Contains(this)) 
       { 
        Status.People.Add(this); 
       } 
       if (StatusId != Status.StatusId) 
       { 
        StatusId = Status.StatusId; 
       } 
      } 
     } 

     private void FixupMembers(object sender, NotifyCollectionChangedEventArgs e) 
     { 
      if (e.NewItems != null) 
      { 
       foreach (Member item in e.NewItems) 
       { 
        item.Person = this; 
       } 
      } 

      if (e.OldItems != null) 
      { 
       foreach (Member item in e.OldItems) 
       { 
        if (ReferenceEquals(item.Person, this)) 
        { 
         item.Person = null; 
        } 
       } 
      } 
     } 

     private void FixupMessages(object sender, NotifyCollectionChangedEventArgs e) 
     { 
      if (e.NewItems != null) 
      { 
       foreach (Message item in e.NewItems) 
       { 
        item.Person = this; 
       } 
      } 

      if (e.OldItems != null) 
      { 
       foreach (Message item in e.OldItems) 
       { 
        if (ReferenceEquals(item.Person, this)) 
        { 
         item.Person = null; 
        } 
       } 
      } 
     } 

     private void FixupNotifications(object sender, NotifyCollectionChangedEventArgs e) 
     { 
      if (e.NewItems != null) 
      { 
       foreach (Notification item in e.NewItems) 
       { 
        item.Person = this; 
       } 
      } 

      if (e.OldItems != null) 
      { 
       foreach (Notification item in e.OldItems) 
       { 
        if (ReferenceEquals(item.Person, this)) 
        { 
         item.Person = null; 
        } 
       } 
      } 
     } 

     private void FixupFromEmails(object sender, NotifyCollectionChangedEventArgs e) 
     { 
      if (e.NewItems != null) 
      { 
       foreach (UpDirEmail item in e.NewItems) 
       { 
        item.FromPerson = this; 
       } 
      } 

      if (e.OldItems != null) 
      { 
       foreach (UpDirEmail item in e.OldItems) 
       { 
        if (ReferenceEquals(item.FromPerson, this)) 
        { 
         item.FromPerson = null; 
        } 
       } 
      } 
     } 

     private void FixupToEmails(object sender, NotifyCollectionChangedEventArgs e) 
     { 
      if (e.NewItems != null) 
      { 
       foreach (UpDirEmail item in e.NewItems) 
       { 
        item.ToPerson = this; 
       } 
      } 

      if (e.OldItems != null) 
      { 
       foreach (UpDirEmail item in e.OldItems) 
       { 
        if (ReferenceEquals(item.ToPerson, this)) 
        { 
         item.ToPerson = null; 
        } 
       } 
      } 
     } 

     private void FixupUpDirEvents(object sender, NotifyCollectionChangedEventArgs e) 
     { 
      if (e.NewItems != null) 
      { 
       foreach (UpDirEvent item in e.NewItems) 
       { 
        item.Person = this; 
       } 
      } 

      if (e.OldItems != null) 
      { 
       foreach (UpDirEvent item in e.OldItems) 
       { 
        if (ReferenceEquals(item.Person, this)) 
        { 
         item.Person = null; 
        } 
       } 
      } 
     } 

     #endregion 
    } 
} 
+0

最初にコードを使用していますか? –

+0

@Jayanthaいいえ、私はまずデータベースを使用しています。 –

答えて

0

私はついにそれを理解しました。これが私のやり方です:

IEnumerable<Restaurants> MyFavoriteRestaurants = person.FavoriteRestaurants.Select(m => m.Restaurants); 
2

あなたはそれについてあまり考える必要はありません、あなたのエンティティクラスを定義した場合、EFはあなたのためにそれを行います

Public class Person{ 
    public int PersonId {get;set;} 
    public string Firsname{get;set;} 
.... 
    public virtual IList<Resturent> Resturents {get;set;} 
} 

Public class Restaurant{ 
    public int ResaurantId {get;set;} 
    public string Name{get;set;} 
.... 
    public virtual IList<Resturent> People{get;set;} 

} 

次に、person.Restaurantsにアクセスすれば、すべてのレストランが表示されます。

+0

私は彼が最初にコードを使用しているとは思わないが、これは他の人に役立つかもしれない。 (BTW、キーはバーチャルナビゲーションの小道具です) – tzerb

+0

うーん...しかし、彼はこれをどのような方法でもできる方法には関係ありません。しかし、そのアプローチは異なるでしょう。 –

+0

実際、私はデータベースファーストを使用しています。私はPOCOエンティティを生成するためにNugetで得たテンプレートを使用しています。同じ結果になるには、どのようなアプローチが必要でしょうか? –

関連する問題