mvc3表之间的关系

发布于 2024-12-02 13:57:16 字数 1819 浏览 6 评论 0原文

我正在 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);

错误是

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

我做错了什么?

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

    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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅紫色的梦幻 2024-12-09 13:57:16

您将在 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)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文