基本的儲存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. 資料會插入資料庫中,當您呼叫SaveChangesThe 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、 附加、 和更新方法傳遞給它們,相關資料> 一節。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 you entity classes.

若實體已經存在資料庫中,則會刪除期間SaveChangesIf 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 (i.e. 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

您可以結合多項新增/更新/移除作業的單一呼叫SaveChangesYou 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();
}