2015-07-11 50 views
-3

林許多關係和EF 6許多使用MVC 5 MVC 5 EF6

有在我的代碼3個表:「信息」,「手機」,「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」。我怎麼能從交界桌上得到它?

+0

它只是數據庫,你有。你有什麼嘗試,什麼錯誤? –

+0

我更新了問題。感謝您的投票:) –

+0

從LeadPhones表中刪除LeadPhoneID列。 –

回答

0

從LeadPhones表中刪除LeadPhoneID列 - 這不是必需的,只會給您帶來問題。刪除類中的所有對LeadPhones的引用。將虛擬財產引向電話(而不是LeadPhones)。 LeadPhones具有潛在客戶的虛擬財產(不是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; } 
} 

我還糾正了多個您的類。鉛與潛在客戶。該類描述了一個單引線,所以它應該被稱爲引線而不是引線。手機vs電話。

+0

我試過了。 我的索引視圖是這樣的: @ Html.DisplayNameFor(型號=> model.Phones.PhoneNo) 查閱即時得到的誤差在索引視圖:「System.Collections.Generic.IEnumerable '不包含'Phones'的定義,也沒有擴展方法'Phones'接受類型'System.Collections.Generic.IEnumerable '的第一個參數可以被找到(你是否缺少using指令或者是一個程序集引用?) –

+0

如果您使用上述類,則該模型應該是'IEnumerable ' –

+0

該模型與您上面提到的完全相同。還有什麼可能是這個問題? –