儲存相關的資料Saving Related Data

除了隔離的實體,您也可以將使用模型中定義的關聯性。In addition to isolated entities, you can also make use of the relationships defined in your model.

提示

您可以檢視這篇文章範例GitHub 上。You can view this article's sample on GitHub.

加入新的實體圖形Adding a graph of new entities

如果您建立新的數個相關的實體,加入其中的內容將會造成太加入其他。If you create several new related entities, adding one of them to the context will cause the others to be added too.

在下列範例中,部落格和三個相關的文章所有插入資料庫。In the following example, the blog and three related posts are all inserted into the database. 找到及加入,因為它們是可透過連線公佈Blog.Posts導覽屬性。The posts are found and added, because they are reachable via the Blog.Posts navigation property.

using (var context = new BloggingContext())
{
    var blog = new Blog
    {
        Url = "http://blogs.msdn.com/dotnet",
        Posts = new List<Post>
        {
            new Post { Title = "Intro to C#" },
            new Post { Title = "Intro to VB.NET" },
            new Post { Title = "Intro to F#" }
        }
    };

    context.Blogs.Add(blog);
    context.SaveChanges();
}

如果您從導覽屬性,已經受到內容追蹤時的實體參考新的實體,將探索到的實體,並插入至資料庫。If you reference a new entity from the navigation property of an entity that is already tracked by the context, the entity will be discovered and inserted into the database.

在下列範例中,post會插入實體,因為它會加入至Posts屬性blog從資料庫中提取的實體。In the following example, the post entity is inserted because it is added to the Posts property of the blog entity which was fetched from the database.

using (var context = new BloggingContext())
{
    var blog = context.Blogs.Include(b => b.Posts).First();
    var post = new Post { Title = "Intro to EF Core" };

    blog.Posts.Add(post);
    context.SaveChanges();
}

變更關聯性Changing relationships

如果您變更實體的導覽屬性,對應的變更將會對資料庫中的外部索引鍵資料行。If you change the navigation property of an entity, the corresponding changes will be made to the foreign key column in the database.

在下列範例中,post實體會更新為隸屬於新blog實體因為其Blog瀏覽屬性設定為指向blogIn the following example, the post entity is updated to belong to the new blog entity because its Blog navigation property is set to point to blog. 請注意,blog就也會插入到資料庫,所以它已經受到內容追蹤實體的導覽屬性所參考的新實體 (post)。Note that blog will also be inserted into the database because it is a new entity that is referenced by the navigation property of an entity that is already tracked by the context (post).

using (var context = new BloggingContext())
{
    var blog = new Blog { Url = "http://blogs.msdn.com/visualstudio" };
    var post = context.Posts.First();

    post.Blog = blog;
    context.SaveChanges();
}

移除關聯性Removing relationships

您可以移除關聯性設為參考導覽null,或移除相關的實體集合的巡覽。You can remove a relationship by setting a reference navigation to null, or removing the related entity from a collection navigation.

移除關聯性可以有相依的實體上的副作用,根據串聯刪除關聯性中設定的行為。Removing a relationship can have side effects on the dependent entity, according to the cascade delete behavior configured in the relationship.

根據預設,針對必要的關聯性,串聯刪除行為設定,將會從資料庫刪除子/相依實體。By default, for required relationships, a cascade delete behavior is configured and the child/dependent entity will be deleted from the database. 選擇性的關聯性的串聯刪除未設定依預設,但將設定外部索引鍵屬性為 null。For optional relationships, cascade delete is not configured by default, but the foreign key property will be set to null.

請參閱必要和選擇性的關聯性若要了解如何設定 requiredness 的關聯性。See Required and Optional Relationships to learn about how the requiredness of relationships can be configured.

請參閱Cascade Delete的 cascade delete 行為方式的詳細工作,他們可以設定的方式明確及如何選取依慣例。See Cascade Delete for more details on how cascade delete behaviors work, how they can be configured explicitly and how they are selected by convention.

在下列範例中,串聯刪除設定之間的關聯性BlogPost,因此post從資料庫刪除實體。In the following example, a cascade delete is configured on the relationship between Blog and Post, so the post entity is deleted from the database.

using (var context = new BloggingContext())
{
    var blog = context.Blogs.Include(b => b.Posts).First();
    var post = blog.Posts.First();

    blog.Posts.Remove(post);
    context.SaveChanges();
}