实体框架多对多关系访问数据

时间:2016-06-24 15:37:56

标签: c# asp.net-mvc entity-framework many-to-many

我正在做第一个涉及多对多关系的项目,我仍然是asp.net的新手,所以请原谅基本问题。我正在处理的项目有许多实体(人员,企业等),每个实体可以有许多电话号码(家庭,工作,小区等)。我必须进行电话号码搜索,该搜索将返回具有与之关联的电话号码的所有实体(在此示例中仅为人)。当我有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; }
    }
}

Join Table

控制器:

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

2 个答案:

答案 0 :(得分:1)

简单地:

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

简而言之,这将选择具有ID为IdentityEntity的相关IdentityNumber实例的所有numberID个实例。

答案 1 :(得分:0)

var entID = from x in person.where(s=>s.**numID**==numID)
            select x.**entityid**;

注意var entID可以将其视为单个变量或多变量,具体取决于查询结果。 我希望它有所帮助。