Entity Framework Basic Query Discover How To Make Basic Queries
In Entity Framework, querying data is executed against the DbSet
properties of the DbContext
.
- The
DbSet
andIDbSet
implement IQueryable, so you can easily write aLINQ
query against the database. - LINQ is a component in the .NET Framework that provides query capability against collections in C# or VB.
- LINQ queries can be written using query syntax or method syntax.
- The EF provider is responsible for translating the LINQ query into the actual SQL to be executed against the database.
The following example loads all the data from Books
table.
using (var context = new BookStore()) { var books = context.Books.ToList(); }
The following example loads a single record from Books
table based on BookId.
using (var context = new BookStore()) { var book = context.Books .Single(b => b.BookId == 1); }
The following example loads all books which contain C# in the title.
using (var context = new BookStore()) { var books = context.Books .Where(b => b.Title.Contains("C#")) .ToList(); }
ZZZ Projects