Results for .Net Core, 7 results
Select, add, edit and delete data in Entity Framework
Asked 1 year 9 months 9 days 22 hours 46 minutes ago, Viewed 3185 times
Find
Find and Agregate.
var author = context.Authors.First();
var author = context.Authors.Find(1);
var products = context.Products.Where(p => p.CategoryId == 1);
var authors = context.Authors.Include(a => a.Books).ToList();
var authors = context.Authors.OrderBy(a => a.LastName);
var groups = context.Products.GroupBy(p => p.CategoryId);
Add
Add or AddRange.
var author = new Author { FirstName = "William", LastName = "Shakespeare" };
context.Authors.Add(author);
context.SaveChanges();
var authors = new List<Author> {
new Author { FirstName = "Stephen", LastName = "King" },
new Author { FirstName = "William", LastName = "Shakespeare" },
new Author { FirstName = "Vincent", LastName = "van Gogh" }
};
context.Authors.AddRange(authors );
context.SaveChanges();
Edit
Update data or update model.
var author = context.Authors.Find(1);
author.FirstName = "William";
context.SaveChanges();
public void Save(Author author)
{
context.Authors.Update(author);
context.SaveChanges();
}
Delete
Remove or RemoveRange.
context.Authors.Remove(context.Authors.Find(1));
context.SaveChanges();
context.Authors.RemoveRange(context.Authors.Where(x => x.FirstName == "William"));
context.SaveChanges();
Like
2