EF Core - Removing a related entity from collection navigation property does not update database

asp.net-core c# entity-framework entity-framework-core

Question

I am trying to understand this sentence:

By default, for required relationships, a cascade delete behavior is configured and the child/dependent entity will be deleted from the database.

found in the "Removing relationships" paragraph in this Microsoft documentation of EF Core: https://docs.microsoft.com/en-us/ef/core/saving/related-data#removing-relationships

On a .Net Core API, with a model of blog and posts as follows

public class Blog
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int BlogId { get; set; }

    public virtual ICollection<Post> Posts { get; set; }
}

public class Post
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PostId { get; set; }

    public int BlogId { get; set; }
    public virtual Blog Blog { get; set; }
}

When I retrieve a blog by id through

[HttpGet("{id}")]
public async Task<IActionResult> GetBlog([FromRoute] int id)
{
    return await context.Blog.Where(b => b.BlogId == id)
                             .Include(b => b.Posts).FirstOrDefaultAsync();
}

I receive in JSON

{
  "BlogId": 1,
  "Posts": [
    {
      "PostId": 1
    },
    {
      "PostId": 2
    }
  ]
}

When I send back to the API the updated following JSON to be persisted in the database

{
  "BlogId": 1,
  "Posts": [
    {
      "PostId": 2
    }
  ]
}

to

[HttpPut("{id}")]
public async Task<IActionResult> PutBlog([FromRoute] int id, [FromBody] Blog blog)
{
    this.context.Update(blog);
    await this.context.SaveChangesAsync();

    return this.Ok();
}

Everything runs fine but no entries are modified.

As the relationship from a post to a blog is required and the related post is removed from the blog collection-of-posts property, shouldn't post with PostId = 1 be deleted from the database?

What really confuses me is that, as soon as I use a many-to-many relationship between blogs and posts, not changing anything else in the logic, the entry in table Blog_Post where PostId = 1 is actually deleted.

1
0
3/11/2019 8:47:48 PM

Popular Answer

Enity Framework delete related entities from principle Entity to Dependent Entity. In your Case Principle is the Blog and Post is the dependent entity. By Deleting Blog related Posts should also be deleted. You also need to decorate with Foreign key attribute, your class in that case will look like this.

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public int BlogForeignKey { get; set; }
    [ForeignKey("BlogForeignKey")]
    public Blog Blog { get; set; }
}

You also need to define delete behaviour, you can do this by using Fluent API setting, Cascade will delete related entity, if you don’t want to delete related entity then use ClientSetNull , it will

model.Entity<Post>().HasOne(p => p.Blog).WithMany(b => b.Posts)
    .HasForeignKey(p => p. BlogForeignKey)
    .OnDelete(DeleteBehavior.Cascade);
0
3/12/2019 3:10:14 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow