Entity Framework 多对多关系访问数据

Entity Framework Many to Many Relationships Accessing Data

我正在做我的第一个多对多关系的项目,我还是 asp.net 的新手,所以请原谅这个基本问题。我正在从事的项目有很多实体(个人、企业等),每个实体可以有很多 phone 个号码(家庭、工作、单元格等)。我必须进行 phone 号码搜索,该号码搜索将 return 所有与该 phone 号码关联的实体(在本例中仅为人)。当我有 NumberID 时,我不知道如何访问 entityID!

型号:

    namespace CaseManagement.Models
{
    using System;
    using System.Collections.Generic;

    public partial class IdentityNumber
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public IdentityNumber()
        {
            this.IdentityEntities = new HashSet<IdentityEntity>();
        }

        public int NumberID { get; set; }
        public System.DateTime DateCreated { get; set; }
        public string TenDigitNumber { get; set; }
        public bool Preferred { get; set; }
        public byte[] RowVersion { get; set; }
        public string ChangedBy { get; set; }
        public int NumberTypeID { get; set; }

        public virtual IdentityNumberType IdentityNumberType { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<IdentityEntity> IdentityEntities { get; set; }
    }
}


    namespace CaseManagement.Models
{
    using System;
    using System.Collections.Generic;

    public partial class IdentityEntity
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public IdentityEntity()
        {
            this.ContactLogs = new HashSet<ContactLog>();
            this.IdentityEntityCasePartyJoins = new HashSet<IdentityEntityCasePartyJoin>();
            this.Files = new HashSet<File>();
            this.IdentityAddresses = new HashSet<IdentityAddress>();
            this.IdentityBusinesses = new HashSet<IdentityBusiness>();
            this.IdentityEmails = new HashSet<IdentityEmail>();
            this.IdentityNumbers = new HashSet<IdentityNumber>();
            this.IdentityPersonNames = new HashSet<IdentityPersonName>();
            this.IdentityPersonPositions = new HashSet<IdentityPersonPosition>();
            this.Notes = new HashSet<Note>();
        }

        public int EntityID { get; set; }
        public int EntityTypeID { get; set; }
        public System.DateTime DateCreated { get; set; }
        public byte[] RowVersion { get; set; }
        public string ChangedBy { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ContactLog> ContactLogs { get; set; }
        public virtual IdentityEntityType IdentityEntityType { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<IdentityEntityCasePartyJoin> IdentityEntityCasePartyJoins { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<File> Files { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<IdentityAddress> IdentityAddresses { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<IdentityBusiness> IdentityBusinesses { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<IdentityEmail> IdentityEmails { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<IdentityNumber> IdentityNumbers { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<IdentityPersonName> IdentityPersonNames { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<IdentityPersonPosition> IdentityPersonPositions { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Note> Notes { get; set; }
    }
}

控制器:

    namespace CaseManagement.Controllers
{
    public class IdentityPersonSelectController : Controller
    {
        private CaseMGMTEntities db = new CaseMGMTEntities();
        // GET: IdentityPersonSelect
        public ActionResult Index(int id)
        {
            var phoneLogs = db.PhoneLogs.Include(p => p.ContactInquirySource).Include(p => p.ContactReason).Include(p => p.EthicsEmployee);
            var recordFromPhoneLog = (from x in phoneLogs
                          where x.PhoneLogID == id
                          select x).First();
            var person = db.IdentityEntities.Include(p => p.IdentityAddresses).Include(p => p.IdentityEmails).Include(p => p.IdentityNumbers).Include(p => p.IdentityPersonNames).Include(p => p.IdentityPersonPositions);



            var sameName = (from x in db.IdentityPersonNames
                            where x.FirstName == recordFromPhoneLog.FirstName.ToLower() &&
                            x.LastName == recordFromPhoneLog.LastName.ToLower()
                            select x).ToList();


            var samePhone = (from x in db.IdentityNumbers
                             where x.TenDigitNumber == recordFromPhoneLog.Phone
                             select x).ToList();

            //going to add the people to this list in the for loop once I can figure out how to access the entityID
            var people = new List<IdentityPersonName>();
            for (int i = 0; i < samePhone.Count; i++ )
            {
                //get the numberID of the current phone number
                var numID = samePhone[i].NumberID;
                //get entityID of that number
                var entID = (from x in person
                             where x.//this is where I am stuck and cannot figure out how to get the entityID from the join table based on the numberID 

                             );
            }



            ViewBag.FN = recordFromPhoneLog.FirstName;
            ViewBag.LN = recordFromPhoneLog.LastName;
            return View(sameName);
        }
    }
}
var entID = from x in person.where(s=>s.**numID**==numID)
            select x.**entityid**;

请注意 var entID,根据查询结果,它可以被视为单个变量或多变量。 希望对你有帮助。

简单:

var entities = db.IdentityEntities.Where(m => m.IdentityNumbers.Any(n => n.NumberID == numberID));

简而言之,这会选择任何 IdentityEntity 个具有相关 IdentityNumber 个实例的实例,其 ID 为 numberID