Wranglerino
Wranglerino

Reputation: 197

Add a list of objects to another class with entity-framework

Im creating a blog i try to "connect" a list of comments to each blog-posts:

public class BlogPost
    {
        public int BlogPostId { get; set; }
        public string Post { get; set; }
    }

comment-class:

public class Comment
    {
        public int CommentId { get; set; }
        public string Comment { get; set; }
    }

How do I specify in my BlogPost-class that I want it to be able to have a list of comments to it?

Upvotes: 0

Views: 1354

Answers (1)

Jerome2606
Jerome2606

Reputation: 955

You need to configure a one-to-many relationship like:

public class BlogPost
{
    ...
    public virtual ICollection<Comment> Comments { get; set; }
}

and

public class Comment
{
    ...
    public virtual Comment Comment { get; set; }
}

You can have more informations on: http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

Upvotes: 1

Related Questions