基本儲存Basic Save

了解如何使用您的內容和實體類別來新增、修改及移除資料。Learn how to add, modify, and remove data using your context and entity classes.

提示

您可以在 GitHub 上檢視此文章的範例 (英文)。You can view this article's sample on GitHub.

加入資料Adding Data

使用 DbSet.Add 方法來新增實體類別的新執行個體。Use the DbSet.Add method to add new instances of your entity classes. 當您呼叫 SaveChanges時,將會在資料庫中插入資料。The data will be inserted in the database when you call SaveChanges.

using (var context = new BloggingContext())
{
    var blog = new Blog { Url = "http://sample.com" };
    context.Blogs.Add(blog);
    context.SaveChanges();

    Console.WriteLine(blog.BlogId + ": " +  blog.Url);
}

提示

相關資料一節中所述,Add、Attach 及 Update 方法對於傳遞給它們的整個實體圖表都適用。The Add, Attach, and Update methods all work on the full graph of entities passed to them, as described in the Related Data section. 或者,您也可以使用 EntityEntry.State 屬性來僅設定單一實體的狀態。Alternately, the EntityEntry.State property can be used to set the state of just a single entity. 例如,context.Entry(blog).State = EntityState.ModifiedFor example, context.Entry(blog).State = EntityState.Modified.

更新資料Updating Data

EF 會針對內容所追蹤的現有實體,自動偵測對實體所進行的變更。EF will automatically detect changes made to an existing entity that is tracked by the context. 這包括您從資料庫載入/查詢的實體,以及先前新增並儲存至資料庫的實體。This includes entities that you load/query from the database, and entities that were previously added and saved to the database.

請直接修改指派給屬性的值,然後呼叫 SaveChangesSimply modify the values assigned to properties and then call SaveChanges.

using (var context = new BloggingContext())
{
    var blog = context.Blogs.First();
    blog.Url = "http://sample.com/blog";
    context.SaveChanges();
}

刪除資料Deleting Data

使用 DbSet.Remove 方法來刪除實體類別的執行個體。Use the DbSet.Remove method to delete instances of your entity classes.

如果實體已經存在於資料庫中,在 SaveChanges 期間將會予以刪除。If the entity already exists in the database, it will be deleted during SaveChanges. 如果尚未將實體儲存至資料庫 (亦即,其追蹤狀態為已新增),就會從內容中將其移除,而在呼叫 SaveChanges 時,便不再將它插入。If the entity has not yet been saved to the database (that is, it is tracked as added) then it will be removed from the context and will no longer be inserted when SaveChanges is called.

using (var context = new BloggingContext())
{
    var blog = context.Blogs.First();
    context.Blogs.Remove(blog);
    context.SaveChanges();
}

在單一 SaveChanges 中執行多個作業Multiple Operations in a single SaveChanges

您可以將多個新增/更新/移除作業結合成對 SaveChanges 的單一呼叫。You can combine multiple Add/Update/Remove operations into a single call to SaveChanges.

注意

對大多數資料庫提供者來說,SaveChanges 為交易式。For most database providers, SaveChanges is transactional. 這意謂著所有作業不是成功就是失敗,作業一律不會只有部分套用的情況。This means all the operations will either succeed or fail and the operations will never be left partially applied.

using (var context = new BloggingContext())
{
    // seeding database
    context.Blogs.Add(new Blog { Url = "http://sample.com/blog" });
    context.Blogs.Add(new Blog { Url = "http://sample.com/another_blog" });
    context.SaveChanges();
}

using (var context = new BloggingContext())
{
    // add
    context.Blogs.Add(new Blog { Url = "http://sample.com/blog_one" });
    context.Blogs.Add(new Blog { Url = "http://sample.com/blog_two" });

    // update
    var firstBlog = context.Blogs.First();
    firstBlog.Url = "";

    // remove
    var lastBlog = context.Blogs.Last();
    context.Blogs.Remove(lastBlog);

    context.SaveChanges();
}