Book Image

Code-First Development with Entity Framework

By : Sergey Barskiy
Book Image

Code-First Development with Entity Framework

By: Sergey Barskiy

Overview of this book

<p>Entity Framework Code-First enables developers to read and write data in a relational database system using C# or VB.NET. It is Microsoft's answer to demand for an ORM from .NET developers.</p> <p>This book will help you acquire the necessary skills to program your applications using Entity Framework. You will start with database configuration and learn how to write classes that define the database structure. You will see how LINQ can be used with Entity Framework to give you access to stored data. You will then learn how to use Entity Framework to persist information in a Relational Database Management System. You will also see how you can benefit from writing ORM-based .NET code. Finally, you will learn how Entity Framework can help you to solve database deployment problems using migrations.</p>
Table of Contents (15 chapters)
Code-First Development with Entity Framework
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Chapter 4: Querying, Inserting, Updating, and Deleting Data


Q1. LINQ supports two types of query syntaxes—method, which looks like any other method calls in your programming language, and Query, which resembles SQL in its appearance.

Q2. The answer is false. If an entity is tracked by the context after retrieval, all changes are tracked individually. Hence, Entity Framework will create an update query that only includes columns/properties touched by the code after the entity in question was retrieved.

Q3. Only the first property in the sort order is specified by the OrderBy method; all subsequent ones should be specified by the ThenBy method calls.

Q4. In order to specify multiple conditions, you need to use logical operators in a single Where method.

Q5. All of the approaches are valid, although you may find that the AddRange is a bit more readable.

Q6. The answer is false. Insert operations are different from other operations. You can add a root entity to its DbSet, and all child entities are assumed to be in new state as well.

Q7. True, since context was not tracking entities prior to the state being set, context has to assume that all properties have been changed.

Q8. The answer is false. If you want to issue a delete query, you need to attach an entity instead of adding it in order to simulate an existing entity in the unchanged state.

Q9. The detached state corresponds to any entity not tracked by the context. Since it is not tracked, DbContext will not look at this entity when SaveChanges is called. Entities in the unchanged state will also not result in any queries, but they are tracked by the context.

Q10. The local property of DbSet will give you access to in-memory data only and will never result in a database query to look for data.