Updating an existing entity is similar to inserting a new one. The Dapper.SimpleCRUD library provides an Update
extension method to update existing data into the database.
The following example updates a single record.
private static void UpdateSingleBook()
{
using (IDbConnection db = new SqlConnection(ConnectionString))
{
Book book = new Book { Id = 1, Title = "Introduction to AI", Category = "Software", AuthorId = 1 };
db.Update<Book>(book);
}
}
If you retrieve all the books from the database, you will see that the above-mentioned book title is updated.
private static List<Book> GetAllBooks()
{
using (IDbConnection db = new SqlConnection(ConnectionString))
{
List<Book> books = db.GetList<Book>().ToList();
return books;
}
}
The following code calls the UpdateSingleBook
method and then retrieves all the books from the database and prints them on the console.
static void Main(string[] args)
{
UpdateSingleBook();
List<Book> books = GetAllBooks();
foreach (var book in books)
{
Console.WriteLine("Title: {0} \t Category: {1}", book.Title, book.Category);
}
}
Let's execute the above code, and you will see the following output.
Title: Introduction to AI Category: Software
Title: Introduction to Computing Category: Software
Title: Romeo and Juliet Category: Humor & Entertainment
Title: The Tempest Category: Fiction
Title: The Winter's Tale : Third Series Category: Fiction
Title: Rich Dad, Poor Dad Category: Economics