efcore 部分更新_基本保存 - EF Core | Microsoft Docs

基本保存Basic Save

10/27/2016

本文内容

了解如何使用上下文和实体类添加、修改和删除数据。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://example.com" };

context.Blogs.Add(blog);

context.SaveChanges();

}

提示

添加、附加和更新方法全部呈现在传递给这些方法的实体的完整关系图上,如相关数据部分中所述。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.Modified。For 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.

只需修改分配给属性的值,然后调用 SaveChanges。Simply modify the values assigned to properties and then call SaveChanges.

using (var context = new BloggingContext())

{

var blog = context.Blogs.First();

blog.Url = "http://example.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://example.com/blog" });

context.Blogs.Add(new Blog { Url = "http://example.com/another_blog" });

context.SaveChanges();

}

using (var context = new BloggingContext())

{

// add

context.Blogs.Add(new Blog { Url = "http://example.com/blog_one" });

context.Blogs.Add(new Blog { Url = "http://example.com/blog_two" });

// update

var firstBlog = context.Blogs.First();

firstBlog.Url = "";

// remove

var lastBlog = context.Blogs.Last();

context.Blogs.Remove(lastBlog);

context.SaveChanges();

}

你可能感兴趣的:(efcore,部分更新)