【问题标题】:Many to Many Relationship in MVC 5 EF6MVC 5 EF6 中的多对多关系
【发布时间】:2015-09-30 03:14:09
【问题描述】:

我正在使用 MVC 5 和 EF 6。

我的代码中有 3 个表:“Leads”、“Phones”、“LeadStatus”和一个连接表“LeadPhones”。 我想在索引视图中显示以下属性:

Leads.FullName
LeadStatus.Status
Phones.PhoneID

我应该如何配置 LeadController?

public partial class Leads
{
    public Leads()
    {
        this.LeadPhones = new HashSet<LeadPhones>();
    }

    [Key]
    public int LeadID { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get { return NamePrefix + " " + FirstName + " " + LastName; } }
    public int LeadStatusID { get; set; } 

    public virtual LeadStatus LeadStatus { get; set; }
    public virtual ICollection<LeadPhones> LeadPhones { get; set; }
}

public partial class Phones
{
    public Phones()
    {
        this.AccountPhones = new HashSet<AccountPhones>();
        this.ContactPhones = new HashSet<ContactPhones>();
        this.EmployeePhones = new HashSet<EmployeePhones>();
        this.LeadPhones = new HashSet<LeadPhones>();
    }

    [Key]
    public int PhoneID { get; set; }           
    public string PhoneNo { get; set; }

    public virtual ICollection<LeadPhones> LeadPhones { get; set; }
}

public partial class LeadPhones
{
    [Key]
    public int LeadPhoneID { get; set; }
    public int LeadID { get; set; }
    public int PhoneID { get; set; }

    public virtual Leads Leads { get; set; }
    public virtual Phones Phones { get; set; }
}

public partial class LeadStatus
{
    public LeadStatus()
    {
        this.Leads = new HashSet<Leads>();
    }

    [Key]
    public int LeadStatusID { get; set; }
    public string Status { get; set; }

    public virtual ICollection<Leads> Leads { get; set; }
}

以下不起作用:

    public ActionResult Index()
    {            
        var leads = db.Leads.Include(l => l.LeadStatus).Include(l => l.LeadPhones);                       

        return View(leads);
    }

主要问题是“PhoneNo”。我怎么能从联结表中得到它?

【问题讨论】:

  • 您拥有的只是数据库。您尝试过什么,出了什么问题?
  • 我更新了问题。谢谢你的反对:)
  • 从 LeadPhones 表中移除 LeadPhoneID 列。

标签: asp.net-mvc linq entity-framework-6 viewmodel


【解决方案1】:

从 LeadPhones 表中删除 LeadPhoneID 列 -- 这不是必需的,只会给您带来问题。删除您的课程中对 LeadPhones 的所有引用。将虚拟财产引导至电话(而非 LeadPhone)。 LeadPhones 对 Leads(不是 LeadPhones)拥有虚拟财产。

完成此操作后,您应该能够:

var leads = db.Leads.Include(l => l.LeadStatus).Include(l => l.Phones);

public partial class Lead
{
    public Lead()
    {
        this.Phones = new HashSet<Phone>();
    }

    [Key]
    public int LeadID { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get { return NamePrefix + " " + FirstName + " " + LastName; } }
    public int LeadStatusID { get; set; } 

    public virtual LeadStatus LeadStatus { get; set; }
    public virtual ICollection<Phone> Phones { get; set; }
} 

public partial class Phone
{
    public Phone()
    {
        this.AccountPhones = new HashSet<AccountPhones>();
        this.ContactPhones = new HashSet<ContactPhones>();
        this.EmployeePhones = new HashSet<EmployeePhones>();
        this.Leads = new HashSet<Lead>();
    }

    [Key]
    public int PhoneID { get; set; }           
    public string PhoneNo { get; set; }

    public virtual ICollection<Lead> Leads { get; set; }
}   
public partial class LeadStatus
{
    public LeadStatus()
    {
        this.Leads = new HashSet<Lead>();
    }

    [Key]
    public int LeadStatusID { get; set; }
    public string Status { get; set; }

    public virtual ICollection<Lead> Leads { get; set; }
}

我还更正了您的多个课程。领先与领先。该类描述了单个潜在客户,因此应将其称为 Lead 而不是 Leads。电话与电话。

【讨论】:

  • 我试过这个。我的索引视图是这样的:@Html.DisplayNameFor(model => model.Phones.PhoneNo) 现在我在索引视图中遇到错误:'System.Collections.Generic.IEnumerable' 不包含“Phones”的定义,并且没有扩展方法“Phones”接受“System.Collections.Generic.IEnumerable”类型的第一个参数(您是否缺少使用指令还是程序集引用?)
  • 如果你使用上面的类,模型应该是IEnumerable&lt;CL_AHR_CRM.Models.Lead&gt;
  • 该模型与您上面提到的完全一样。还有什么问题?
  • 该错误消息应该告诉您生成它的行和文件,然后查看它,因为Models.Leads 无效。应该是Models.Lead
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多