【问题标题】:ASP.Net MVC 1 to many binding doesn't workASP.Net MVC 1 到多个绑定不起作用
【发布时间】:2013-12-20 16:42:53
【问题描述】:

我是 MVC 新手,遇到了这个烦人的问题。

我正在做一个小项目来更好地理解 MVC。

我以代码优先的方式工作。

我创建了这两个类:

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

        [Required]
        public string UserName { get; set; }

        [Required]
        public string Title { get; set; }

        [Required]
        public string Text { get; set; }

        [Required]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
        public DateTime Date { get; set; }

        [Required]
        public PostCategoryType Category { get; set; }

        [Required]
        public string PictureUrl { get; set; }

        [Required]
        public string VideoUrl { get; set; }

        public virtual List<UserComment> Comments { get; set; }
    }

public class UserComment
    {
        [Required]
        public int ID { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        public string WebSite { get; set; }
        [Required]
        public string Comment { get; set; }
        [Required]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
        public DateTime Date { get; set; }
    }

public class MyStoreDbContext : DbContext
    {
        public DbSet<Post> Posts { get; set; }
        public DbSet<UserComment> UsersComments { get; set; }
    }

为了在实体框架中将类生成到表中,我只是添加了控制器 - 每个类一个,然后运行程序并生成两个表。

实体框架中生成的表格如下所示:

CREATE TABLE [dbo].[Posts] (
    [ID]         INT            IDENTITY (1, 1) NOT NULL,
    [UserName]   NVARCHAR (MAX) NOT NULL,
    [Title]      NVARCHAR (MAX) NOT NULL,
    [Text]       NVARCHAR (MAX) NOT NULL,
    [Date]       DATETIME       NOT NULL,
    [Category]   INT            NOT NULL,
    [PictureUrl] NVARCHAR (MAX) NOT NULL,
    [VideoUrl]   NVARCHAR (MAX) NOT NULL,
    CONSTRAINT [PK_dbo.Posts] PRIMARY KEY CLUSTERED ([ID] ASC)
);



CREATE TABLE [dbo].[UserComments] (
    [ID]      INT            IDENTITY (1, 1) NOT NULL,
    [Name]    NVARCHAR (MAX) NOT NULL,
    [Email]   NVARCHAR (MAX) NOT NULL,
    [WebSite] NVARCHAR (MAX) NOT NULL,
    [Comment] NVARCHAR (MAX) NOT NULL,
    [Date]    DATETIME       NOT NULL,
    [Post_ID] INT            NULL,
    CONSTRAINT [PK_dbo.UserComments] PRIMARY KEY CLUSTERED ([ID] ASC),
    CONSTRAINT [FK_dbo.UserComments_dbo.Posts_Post_ID] FOREIGN KEY ([Post_ID]) REFERENCES [dbo].[Posts] ([ID])
);


GO
CREATE NONCLUSTERED INDEX [IX_Post_ID]
    ON [dbo].[UserComments]([Post_ID] ASC);

我有一个控制器,它的数据成员为:

私有 MyStoreDbContext db = new MyStoreDbContext ();

我希望我的帖子和用户 cmets 来自哪里。

现在解决问题: 当我访问时:

foreach(Post post in db.Posts)
{
  post.Comments <------ This is always null
}

例如,我尝试插入一条 id 为 1 的帖子记录。 比我插入到 post_id 为 1 的 UserComments 中。(都使用 VS Server Explorer 手动); 当我尝试上面的代码时,cmets 总是为空。

我什至尝试了不同的方法,在调试时手动插入:

UserComment comment = new UserComment... // Initialization
Post post = new Post.. // Initializing all but Comments
post.Comments = new List<UserComment>();
post.Comments.Add(comment);
db.Comments.Add(comment);
db.Posts.Add(post);
db.SaveChanges();

毕竟,每个帖子的 cmets 属性都是空的。

我错过了什么? 请帮助我,因为它让我发疯,并且在线上其他人对这个问题的解决方案就是这样,我无法指出问题。

提前感谢所有帮助者!

【问题讨论】:

  • 如果我的回答对你有用,请告诉我。

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


【解决方案1】:

您需要在UserComment 类中添加IDPost,如下所示:

public class UserComment
    {
        [Required]
        public int ID { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        public string WebSite { get; set; }
        [Required]
        public string Comment { get; set; }
        [Required]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
        public DateTime Date { get; set; }  

        public int PostID { get; set; }

       [ForeignKey("PostID")]
       public virtual Post Post { get; set; }       

    }

然后,首先创建Post 的实例,如下所示:

var newPost = new Post{
   UserName = "John",
   //
}

db.Posts.Add(newPost );
db.SaveChanges();

之后,创建UserComment的实例,将newPostID赋值给它,见下图:

var comment  = new UserComment
{
   Name  = "Some one",
   //
   PostID = newPost.ID
}
newPost.Comments.Add(comment);
db.SaveChanges();

注意: 您可以通过以下链接了解更多信息:

http://codeblog.shawson.co.uk/entity-framework-4-1-code-first-with-one-to-many-relationship-code-example/

【讨论】:

  • 非常感谢林。所缺少的是这一行: public virtual Post Post { get;放; } 这一行解决了我的问题
猜你喜欢
  • 2015-01-05
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
相关资源
最近更新 更多