0

假设我在形成多对多关系的数据库中有以下表。在使用 EF 4.1 的 ASP.Net MVC 项目中,我有相应的 POCO 实体和一个存储库层来访问数据库。

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

如果我有 PersonId,列出此人最喜欢的所有餐厅名称的最佳方式是什么?

或者我将如何使用存储库层根据人员的 Id 初始化此 Enumerable?

IEnumerable MyFavoriteRestaurants = ???

我知道我可以使用 foreach 在 Person.FavoriteRestaurants 集合上循环并逐个检索每个 FavoriteRestaurant.Restaurant,但我想知道是否有更优雅的方法可以在一行代码中执行此操作。

更新

这是我项目中 Person POCO 实体类的示例。此代码由从此处下载的 Microsoft 模板生成:

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
    }
}
4

2 回答 2

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获得所有餐厅。

于 2012-04-29T05:18:03.830 回答
0

我终于弄明白了。这就是我的做法:

IEnumerable<Restaurants> MyFavoriteRestaurants = person.FavoriteRestaurants.Select(m => m.Restaurants);
于 2012-07-09T17:15:02.467 回答