I am new to MVC and I've got this annoying problem.
I am doing a small project to understand MVC better.
I am working in a code-first way.
I've created these two classes:
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; }
}
in order for the classes to be generated into tables at the entity framework I simply added controllers - one for each class and than ran the program and it generated the two tables.
The generated tables at the entity framework looks like this:
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);
I have a controller which have a data member of :
private MyStoreDbContext db = new MyStoreDbContext ();
in which I want my posts and user comments from.
Now to the problem: When I access :
foreach(Post post in db.Posts)
{
post.Comments <------ This is always null
}
I've tried inserting a record of post with id 1 for example. Than I inserted to the UserComments in which the post_id is the 1. (Both manually with VS Server Explorer); when I try the code above the comments are always null.
I even tried different approach in which I manually inserted at debug time this:
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();
After all that, each and every post's comments property is null.
What am I missing? Please help me as it is driving me crazy and accroding to other people's solutions to this problem online is exactly like this I cannot point out the problem.
Thanks in advance to all the helpers!
You need to add ID
of Post
in the UserComment
class, like below:
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; }
}
Then, Create a instance of Post
first, like below:
var newPost = new Post{
UserName = "John",
//
}
db.Posts.Add(newPost );
db.SaveChanges();
After that, create a instance of UserComment
, assign the ID
of newPost
to it, see below:
var comment = new UserComment
{
Name = "Some one",
//
PostID = newPost.ID
}
newPost.Comments.Add(comment);
db.SaveChanges();
Note: You can learn more about this at below link:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.