儲存相關資料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();
}

提示

請使用 EntityEntry.State 屬性來僅設定單一實體的狀態。Use the EntityEntry.State property to set the state of just a single entity. 例如,context.Entry(blog).State = EntityState.ModifiedFor example, context.Entry(blog).State = EntityState.Modified.

如果您從內容所追蹤實體的導覽屬性參考新的實體,系統將會探索到該實體並插入到資料庫中。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 實體,因為該實體已新增至擷取自資料庫 blog 實體的 Posts 屬性。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.

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

如需有關串聯刪除行為如何運作、如何明確設定這些行為及如何依慣例選取這些行為的更多詳細資料,請參閱串聯刪除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();
}