如何在实例表中为多对多关系

时间:2017-09-04 19:53:19

标签: c# asp.net-mvc entity-framework linq asp.net-mvc-5

我是LINQ和Enitty Framework的初学者,我被困在一个地方。我和#34;推荐"之间存在很多关系。和"推荐人"表。我把它命名为" ReferralInstance"表

推荐模式

 public class Referral
    {
        public Referral()
        {
            ReferralInstances = new Collection<ReferralInstance>();
        }

        [Key]
        public int ReferralId { get; set; }
        public ICollection<ReferralInstance> ReferralInstances { get; set; }
    }

推荐人型号:

Public class ApplicationUser
  {
       public ApplicationUser()
       {
            ReferralInstances = new Collection<ReferralInstance>();
       }
       [Key]
       public string ReferrerId {get;set;}
       public ICollection<ReferralInstance> ReferralInstances { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
  }

实例表

public class ReferralInstance
{
    [Key]
    public int ReferralInstanceId { get; set; }
    public bool ReferralStatus { get; set; }

    public int ReferralId { get; set; }
    public virtual Referral Referral { get; set; }

    public string ReferrerId { get; set; }
    public virtual ApplicationUser Referrer { get; set; }
}

Fluent API:

 dBModelBuilder.Entity<Referral>()
                  .HasMany(r => r.ReferralInstances)
                  .WithRequired(r => r.Referral)
                  .HasForeignKey(r => r.ReferralId);

  dBModelBuilder.Entity<ApplicationUser>()
                 .HasMany(r => r.ReferralInstances)
                 .WithRequired(r => r.Referrer)
                 .HasForeignKey(r => r.ReferrerId).
                 WillCascadeOnDelete(false);

现在从UI我将得到一个输入ReferrerId,因为我们说ABC。我想获得将FK作为ABC的ReferralInstances列表,然后我想得到所有相应的推荐列表。

我的新手尝试如下:

public ActionResult ReferredCandidates()
        {
            var referrerId = User.Identity.GetUserId();
            // trying to get list of "ReferralInstance"
            var referralInstanceList = _context.ReferralInstances.Where(r => (r.ReferrerId == referrerId)).Select(r => r.ReferralId);
            // Now I want to get all the referrals that match that id list I got above
            // what to do here
            // var referralss = _context.Referrals.Where(r => (r.ReferrerId  r.ReferralInstances.);

            return View(referrals);
        } 

实施例

推荐表

ReferralId       Text
1                HelloWorld
2                FooBar
3                HelloFoo

ReferralInstance表:

 PK     ReferralId       ReferrerId

  1         1              ABC   
  2         1              DEF
  3         1              XYZ
  4         2              ABC
  5         3              DEF

推荐人表

    ReferrerId           Flag
    ABC                   USA   
    XYZ                   INDIA
    DEF                   UK

现在假设我输入为ABC,然后我想要下面的结果

Result Set

ReferralId         Text
1                 HelloWorld
2                  FooBar

1 个答案:

答案 0 :(得分:1)

您可以直接查询导航属性ReferralInstance.Referral

var referrals = _context.ReferralInstances
    .Where(r => r.ReferrerId == referrerId)
    .Select(r => r.Referral);

也许您想将Distinct()添加到查询中。