Linq EF 4.1,数据库优先和多对多关系-如何获取所有子对象?

Linq EF 4.1,数据库优先和多对多关系-如何获取所有子对象?,linq,entity-framework-4,repository,many-to-many,database-first,Linq,Entity Framework 4,Repository,Many To Many,Database First,假设我在一个数据库中有以下表,它们形成了多对多关系。在一个使用EF4.1的ASP.NETMVC项目中,我有相应的POCO实体和一个存储库层来访问数据库 People: PersonId PK, Firsname, Lastname FavoriteRestaurants: ID PK, PersonId, RestaurantId Restaurants: ResaurantId PK, Name 如果我有一个PersonId,列出这个人最喜欢的餐厅名称的最佳方式是什么 或者,如何使用存储库层

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

People: PersonId PK, Firsname, Lastname
FavoriteRestaurants: ID PK, PersonId, RestaurantId
Restaurants: ResaurantId PK, Name
如果我有一个PersonId,列出这个人最喜欢的餐厅名称的最佳方式是什么

或者,如何使用存储库层根据个人Id初始化此枚举

IEnumerable MyFavoriteRestaurants=

我知道我可以使用foreach循环Person.FavoriteRestaurants collections检索每个FavoriteRestaurant.Restaurant,但我想知道是否有更优雅的方法可以在一行代码中实现这一点

更新

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

//------------------------------------------------------------------------------
// 
//此代码是从模板生成的。
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
// 
//------------------------------------------------------------------------------
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Collections.Specialized;
命名空间UpDir.Domain
{
公共部分类人员:EntityBase
{
#区域基本属性
公共虚拟int PersonId
{
得到;
设置
}
公共虚拟字符串名
{
得到;
设置
}
公共虚拟字符串名称
{
得到;
设置
}
公共虚拟字符串Lastname
{
得到;
设置
}
公共虚拟字符串昵称
{
得到;
设置
}
公共虚拟字符串加密邮件
{
得到;
设置
}
公共虚拟字符串照片
{
得到;
设置
}
公共虚拟短状态ID
{
获取{return\u statusId;}
设置
{
如果(_statusId!=值)
{
if(Status!=null&&Status.StatusId!=value)
{
状态=空;
}
_statusId=值;
}
}
}
专用短_statusId;
#端区
#区域导航属性
公共虚拟ICollection成员
{
得到
{
if(_members==null)
{
var newCollection=newfixupcollection();
newCollection.CollectionChanged+=FixupMembers;
_成员=新集合;
}
返回(u)成员;;
}
设置
{
如果(!ReferenceEquals(_成员,值))
{
var-previousValue=_成员作为FixupCollection;
如果(上一个值!=null)
{
previousValue.CollectionChanged-=FixupMembers;
}
_成员=价值;
var newValue=作为FixupCollection的值;
if(newValue!=null)
{
newValue.CollectionChanged+=FixupMembers;
}
}
}
}
私人ICollection成员;
公共虚拟ICollection消息
{
得到
{
如果(_messages==null)
{
var newCollection=newfixupcollection();
newCollection.CollectionChanged+=FixupMessages;
_messages=newCollection;
}
返回消息;
}
设置
{
如果(!ReferenceEquals(_消息,值))
{
var-previousValue=\u消息作为FixupCollection;
如果(上一个值!=null)
{
previousValue.CollectionChanged-=修复消息;
}
_信息=价值;
var newValue=作为FixupCollection的值;
if(newValue!=null)
{
newValue.CollectionChanged+=FixupMessages;
}
}
}
}
私有ICollection\u消息;
公共虚拟ICollection通知
{
得到
{
如果(_notifications==null)
{
var newCollection=newfixupcollection();
newCollection.CollectionChanged+=修复通知;
_通知=新收集;
}
返回通知;
}
设置
{
如果(!ReferenceEquals(_通知,值))
{
var previousValue=\u通知作为FixupCollection;
如果(上一个值!=null)
{
previousValue.CollectionChanged-=修复通知;
}
_通知=值;
var newValue=作为FixupCollection的值;
if(newValue!=null)
{
newValue.CollectionChanged+=修复通知;
}
}
}
}
私人ICollection\u通知;
公共虚拟状态
//------------------------------------------------------------------------------
// <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
    }
}
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;}

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