【问题标题】:Relationships between tables mvc3表 mvc3 之间的关系
【发布时间】:2011-09-03 19:44:23
【问题描述】:

我正在使用 MVC 3 构建网站。

我首先在现有数据库中使用 EF 代码。

我在模型中的 ET 是这样的:

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

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

}

正如您在我的第一个表中看到的那样,我与另一个名为 PageContent 的表有关系。

在我的 Pages 课程中我有这个代码

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

现在,当我尝试将新 pageContent 添加到新页面时出现错误。

查看此代码

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

错误是

对象引用未设置为对象的实例。

我做错了什么?

【问题讨论】:

  • 您在哪一行得到对象引用错误?如果它位于p.PageContent.Add(pageContent);,那么您确定 p.PageContent 确实已设置并且不是 NULL

标签: asp.net-mvc-3 entity-framework-4 ef-code-first icollection


【解决方案1】:

您将在 p.PageContent.Add(pageContent); 获得 NRE,因为该集合尚未初始化。在Pages类的构造函数中初始化集合。

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

或者在将对象添加到集合之前

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

p.PageContent.Add(pageContent);

您应该考虑使用正确的命名约定(例如Page 而不是Pages)。

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    相关资源
    最近更新 更多