Page 1 of 1

Is EF "aware" of relationships for Insert and Delete

Posted: Wed 18 Jan 2017 01:21
by KW
Given:

Code: Select all

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public Blog Blog { get; set; }
}
Let's say Blogs 'Update' and 'Delete' relationship to Posts is set to 'STRICT'.

If you mark the entity object Blog and Posts as deleted and hit save, is the framework smart enough to know you need to delete the Posts first before it deletes the Blog?

Conversely, if you Add a Blog and Post to a DBCONTEXT, does it know that it needs to create the Blog first before it creates the Posts?

Re: Is EF "aware" of relationships for Insert and Delete

Posted: Fri 20 Jan 2017 09:14
by Shalex
KW wrote:If you mark the entity object Blog and Posts as deleted and hit save, is the framework smart enough to know you need to delete the Posts first before it deletes the Blog?
Yes, it is.
KW wrote:Conversely, if you Add a Blog and Post to a DBCONTEXT, does it know that it needs to create the Blog first before it creates the Posts?
Yes, it does.