开发者

Relationships between tables mvc3

I'm building website in MVC 3.

i am using EF code first in existing database.

my ET inside the model look like that:

 public class Pages
{
    [Required]
    public int ID { get; set; }

    public int ParentID { get; set; }

    [Required]
    public int PageType { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [DisplayName("כותרת")]
    public string Title { get; set; }

  开发者_如何学C  public string SearchWords { get; set; }

    public string Leng { get; set; }

    public int? Sort { get; set; }

    public string Modules { get; set; }

    [ForeignKey("PageType")]
    public virtual PagesType Type { get; set; }

    public virtual IEnumerable<PagesType> Types { get; set; }

    [ForeignKey("PageID")]
    public ICollection<PageContent> PageContent { get; set; }

    [ForeignKey("PageID")]
    public virtual ICollection<ImagesTable> Images { get; set; }


}

public class PageContent
{
    public int ID { get; set; }

    public int PageID { get; set; }

    public string Header { get; set; }

    public string Text { get; set; }

    [ForeignKey("ID")]
    public virtual ICollection<Pages> Pages { get; set; }

}

as you see in my fist table that cold Pages i have a relationship to another table that named PageContent.

in my Pages class i had this code

[ForeignKey("PageID")]
    public ICollection<PageContent> PageContent { get; set; }

now, when i trying to add new pageContent into new page i get an error.

see this code

public ActionResult AddPage(PageModel page)
    {
        SystemLogic cmd = new SystemLogic();

        page.Leng = "he";
        Models.Pages p = new Pages();

        p.ParentID = page.ParentID;
        PageContent pageContent = new PageContent();
        pageContent.Text = page.Content;

        p.PageContent.Add(pageContent);

The error is

Object reference not set to an instance of an object.

What i did wrong?


You will get the NRE at p.PageContent.Add(pageContent); because the collection is not initialized. Initialize the collections inside the constructor of Pages class.

public class Pages
{
    public Pages()
    {
        PageContent = List<PageContent>();
        Images = List<ImagesTable>();
    }

    [Required]
    public int ID { get; set; }

    public int ParentID { get; set; }

    [Required]
    public int PageType { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [DisplayName("כותרת")]
    public string Title { get; set; }

    public string SearchWords { get; set; }

    public string Leng { get; set; }

    public int? Sort { get; set; }

    public string Modules { get; set; }

    [ForeignKey("PageType")]
    public virtual PagesType Type { get; set; }

    public virtual IEnumerable<PagesType> Types { get; set; }

    [ForeignKey("PageID")]
    public ICollection<PageContent> PageContent { get; set; }

    [ForeignKey("PageID")]
    public virtual ICollection<ImagesTable> Images { get; set; }
}

Or before you add objects to the collection

if (p.PageContent == null) p.PageContent = new List<PageContent>();

p.PageContent.Add(pageContent);

You should consider using proper naming conventions(eg Page instead of Pages).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜