I have two simple models from which I am creating database tables with the help of Entity Framework:
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public Blog()
{
Posts = new Collection<Post>();
}
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
// foreign key of Blog table
public int BlogId { get; set; }
}
Now In my DB Context I have DBSet to generate database tables:
public DbSet<Blog> Blogs { get; set; }
Database tables are generated as expected, but now the question, how do I insert new Blog with posts to the DB. I've tried something like this:
Blog blog = context.Blogs.FirstOrDefault(b => b.Title == blogTitle);
// no blog with this title yet, create a new one
if (blog == null) {
blog = new Blog();
blog.Title = blogTitle;
Post p = new Post();
p.Title = postTitle;
p.Content = "some content";
blog.Posts.Add(p);
context.Blogs.Add(blog);
}
// this blog already exist, just add post to it
else
{
Post p = new Post();
p.Title = postTitle;
p.Content = "some content";
context.Blogs.FirstOrDefault(b => b.Title == blogTitle).Posts.Add(p);
}
context.SaveChanges();
As you can see I am not touching Id and BlogId, as those should be generated by EntityFramework automatically.
However, using this code, only my Blog is getting inserted into database, next time I'll try to execute the same code it will tell me that the Posts collection of my blog is empty.
Am I doing something wrong? Is there a better practice for doing insertion/update of records to the DB with one to many relationship?
Thank you
UPDATE:
Thanks to answers, I was able to insert both Blog and Post into my DB, however, my post is still not linked to the particular Blog, BlogId inside Post table is always 0.
Do I need to increment it manually or maybe some kind of attribute?
A self-referencing many-to-many relationship exists when a given record in the table can be related to one or more other records within the table and one or more records can themselves be related to the given record.
{property}' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.
To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship.
Should be something like:
Blog blog = context.Blogs.FirstOrDefault(b => b.Title == blogTitle);
// no blog with this title yet, create a new one
if (blog == null) {
blog = new Blog();
blog.Title = blogTitle;
blog.Posts = new Collection<Post>();
context.Blogs.Add(blog);
}
Post p = new Post();
p.Title = postTitle;
p.Content = "some content";
blog.Posts.Add(p);
context.SaveChanges();
Furthermore I would delete Posts initialization in the constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With